【发布时间】:2023-03-26 17:04:01
【问题描述】:
我最近一直在学习如何使用tkinter,我想使用按钮在两个不同的canvas(更合适的术语)之间切换。但是,每当我在按钮之间单击以切换帧时,屏幕似乎并没有清除。有什么办法解决这个问题?
#Imports
import tkinter as tk
#master means the variable
class App(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master.title("Testing") #Setting the title
self.master.minsize(800, 600) #The minimum size it can be
self.canvas = tk.Canvas(self, bg="black")
self.section1()
def section2(self):
self.pack(fill=tk.BOTH, expand=1)
#Creating the canvas and drawing a line
self.canvas.create_line(15, 25, 200, 25, fill="red")
#Going back to the "main menu"
self.exit = tk.Button(self, text="exit", fg="red", command=self.section1)
self.exit.place(x=0, y=50)
self.canvas.pack(fill=tk.BOTH, expand=1)
def section1(self): #Main menu
self.pack(fill=tk.BOTH, expand=1)
self.button = tk.Button(self, text="See Line", fg="red", command=self.section2)
self.button.place(x=0, y=0)
#Exit button
self.exit = tk.Button(self, text="exit", fg="red", command=self.master.destroy)
self.exit.place(x=0, y=25)
window = App() #Declaring the window
window.mainloop()
【问题讨论】:
-
我在你的代码中只看到一个画布。你想切换到什么?
-
section1()和section2()不是“帧”。
标签: python tkinter tkinter-canvas