当我点击Tic Tac Toe按钮时,它应该启动一个新的窗口但不显示按钮并且不会出现任何错误。
这两段代码分为两个不同的脚本。
当我在自己的脚本中运行Tic Tac Toe时,它会运行并显示所有按钮。import tkinter as tk
import hangman
import playgame
class GAME_APP(tk.Tk):
def __init__(self,*args,**kwargs):
tk.Tk.__init__(self,*args,**kwargs)
container=tk.Frame(self)
container.pack(side='top',fill='both',expand=True)
container.grid_rowconfigure(0,weight=1)
container.grid_columnconfigure(0,weight=1)
self.frames = {}
F=StartPage
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text="GAME APP", font=('Ravie',50,'bold italic'),fg='Dark Blue',bg='Light Pink',padx=20,pady=10,justify='center')
label.grid(padx=80,pady=50)
button = tk.Button(self, text="Tic Tac Toe",bg='Light Green',fg='Purple',bd=8,justify='center',font=('Broadway',18,'bold italic'),command=self.TTT)
button.grid(padx=30,pady=30)
button2 = tk.Button(self, text="Hangman",bg='Light Green',fg='Purple',bd=8,justify='center',font=('Broadway',18,'bold italic'),command=self.hangman_win)
button2.grid(padx=30,pady=30)
def hangman_win(self):
bb=hangman.mainwindow()
def TTT(self):
tt=playgame.GUI()
app=GAME_APP()
app.title('Game App')
app.geometry('700x500')
app.mainloop()
这是Tic Tac Toe的GUI(最初有9个按钮,但删除它们以保持代码短。)class GUI(playGame):
def __init__(self):
import tkinter as tk
self.home=tk.Tk()
self.home.title("Tic Tac Toe")
self.home.geometry("160x300")
w,h=6,3
self.c1r1=tk.Button(text='',width=w, height=h, command=lambda: self.userTurn(self.c1r1))
self.c1r1.pack()
self.c1r2=tk.Button(text='',width=w, height=h, command=lambda: self.userTurn(self.c1r2))
self.c1r2.pack()
self.announce=tk.Label(text='No winner yet',width=w, height=h)
self.announce.pack()
self.home.mainloop()