在同一tkinter文件的另一个窗口中编写代码

时间:2018-08-04 05:58:27

标签: python tkinter

在我的项目中,我创建了一个带有两个按钮的窗口,两个按钮可以正常工作,但是随后我创建了第二个窗口,并且无法添加任何小部件,例如标签,画布等。我尝试使用command = newwindow,但是它没有似乎不适合我。

这是下面的代码:

from tkinter import *
from tkinter import ttk
root = Tk()
global button1

def openwindow():
openwindow = Tk()
button1.destroy()

button1 = Button(root, text="Play", command=openwindow)

button1.grid(row=0, column=0, padx=2)


class Joshuasgame:

      def __init__(self, master):
        frame = Frame(master)
        frame.grid()

     self.quitButton = Button(frame, text="Quit", command=frame.quit, fg="red")
     self.quitButton.grid(row=2, column=0, padx=2)


b = Joshuasgame(root)


root.mainloop()

#Supposed to be second window


from tkinter import *

class root(Tk.frame):


   def __init__(self, parent, controller):
         Tk.Frame.__init__(self,parent)


 label_1 = ttk.Label(self, text="Level 1, Round 1", command=lambda: 
 controller.show_frame(openwindow))
 label_1.grid(sticky=N)


 openwindow = Tk()
 openwindow.geometry('640x480')

 c = Canvas(openwindow, height=250, width=300, bg="white")
 c.pack(side=TOP)

 #                      x1, y1, x2, y2
r = c.create_rectangle( 30,  50, 80, 100, fill="yellow",
                   activefill="grey", disabledfill="grey")
f = c.create_rectangle( 30, 120, 80,170, fill="red",
                   activefill="grey", disabledfill="grey")
p = c.create_rectangle( 30,190, 80,240, fill="green",
                   activefill="grey", disabledfill="grey")



root.mainloop()

0 个答案:

没有答案