Найти - Пользователи
Полная версия: Помогите доделать код
Начало » Центр помощи » Помогите доделать код
1
Egikus
 from tkinter import *
root = Tk()
def oval_func(event):
c.delete(oval)
c.create_text(80, 50, text='Овал')
def rect_func(event):
c.delete(tril)
c.create_text(230, 50, text='Прямогульник')
def triangle(event):
c.delete(trian)
c.create_text(380, 50, text='Треугольник')
c = Canvas(width=460, height=100, bg='grey')
c.pack()
oval = c.create_oval(30,10,130,80, fill='orange')
tril = c.create_rectangle(180,10,280,80,fill='lightgreen')
trian = c.create_polygon(330,80,380,10,430,80,fill='white',outline='black')
c.tag_bind(oval,'<Button-1>', oval_func)
c.tag_bind(tril, <Button-1>, rect_func)
c.tag_bind(trian, <Button-1>, triangle)
root.mainloop()

Здесь, при нажатии на фигуру, высвечивается текст
А мне хочется, чтобы после высвечивания текста, при нажатии на него, высвечивалась та же самая фигура
xam1816
  
import tkinter as tk
class FigureFrame(tk.Frame):
    def __init__(self, master):
        super().__init__(master)
        self.c = tk.Canvas(self, width=200, height=200, bg='grey')
        self.c.pack(fill=tk.BOTH, expand=True)
        self.c.bind('<Button-1>', self.on_click)
        self.text = self.create_text()
        self.figure = self.create_figure()
        self.figure_is_open = False
    def create_text(self):
        raise NotImplementedError()
    def create_figure(self):
        raise NotImplementedError()
    def on_click(self, *args):
        if self.figure_is_open:
            self.show_name_figure()
            self.figure_is_open = False
        else:
            self.show_figure()
            self.figure_is_open = True
    def show_figure(self):
        self.c.itemconfigure(self.text, state="hidden")
        self.c.itemconfigure(self.figure, state="normal")
    def show_name_figure(self):
        self.c.itemconfigure(self.text, state="normal")
        self.c.itemconfigure(self.figure, state="hidden")
class OvalFrame(FigureFrame):
    def create_text(self):
        return self.c.create_text(100, 100, text='Овал')
    def create_figure(self):
        return self.c.create_oval(20, 50, 170, 150, fill='orange', state='hidden')
class RectFrame(FigureFrame):
    def create_text(self):
        return self.c.create_text(100, 100, text='Прямоугольник')
    def create_figure(self):
        return self.c.create_rectangle(20, 50, 170, 150, fill='lightgreen', state='hidden')
class TriangleFrame(FigureFrame):
    def create_text(self):
        return self.c.create_text(100, 100, text='Треугольник')
    def create_figure(self):
        return self.c.create_polygon(100, 50, 50, 150, 150, 150, fill='white', outline='black', state='hidden')
root = tk.Tk()
OvalFrame(root).pack(fill=tk.BOTH, expand=True, side='left')
RectFrame(root).pack(fill=tk.BOTH, expand=True, side='left')
TriangleFrame(root).pack(fill=tk.BOTH, expand=True, side='left')
root.mainloop()
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB