简体   繁体   English

如何在 Tkinter 中使用顶级小部件传递 class

[英]How to pass a class with Toplevel widget in Tkinter

在此处输入图像描述

I have a readymade game( 2048 ), where the game starts without a welcome window, so, I have just made a welcome window with a bunch of buttons like New Game and AI mode, now when I click the New Game button, I expect to get a new window where the game can be played.我有一个现成的游戏( 2048 ),游戏开始时没有欢迎 window,所以,我刚刚制作了一个欢迎 window,带有一堆按钮,如新游戏和 AI 模式,现在当我点击新游戏按钮时,我希望获得可以玩游戏的新 window。 However, the game displays at the bottom of the main window, and another window that is supposed to display the actual game displays nothing.然而,游戏显示在主 window 的底部,而另一个应该显示实际游戏的 window 什么也没有显示。

# mainwindow.py file:
from tkinter import *
from PIL import ImageTk, Image
import game_2048
 
root = Tk()
 
root.iconbitmap('unnamed.ico')
root.title('2048')
 
 
bg = ImageTk.PhotoImage(Image.open("welcome.png"))
 
my_canvas = Canvas(root, width=780, height=550)
my_canvas.grid()
 
my_canvas.create_image(0, 0, image=bg, anchor=NW)
 
button1 = Button(root, text="New Game", fg="black", bg="#ddf0d0", padx=3,
                 pady=3, font=('Helvetica', '12', 'bold'), activebackground="#94d3c3", command=lambda: game_2048.mains(root)
                 )
button2 = Button(root, text="AI Mode", fg="black", bg="#ddf0d0",
                 padx=3, pady=3, font=('Helveica', '12', 'bold'), activebackground="#94d3c3")
 
button1_window = my_canvas.create_window(10, 10, anchor=NW, window=button1)
button2_window = my_canvas.create_window(120, 10, anchor=NW, window=button2)

root.mainloop()

And I have tried to modify the game_2048 file ie 2048 game like this:我曾尝试修改 game_2048 文件,即2048游戏,如下所示:

def mains(root):
    Top = Toplevel(root)
    l1 = Button(Top, command=lambda:Game())
    l1.pack()
 
 
class Game(tkinter.Frame):
    def __init__(self):
        tkinter.Frame.__init__(self)
        self.grid()
        self.master.title('2048')
 
        self.main_grid = tkinter.Frame(
            self, bg=c.GRID_COLOR, bd=3, width=400, height=400)
        self.main_grid.grid(pady=(80, 0))
        self.make_GUI()
        self.start_game()
 
        self.master.bind("<Left>", self.left)
        self.master.bind("<Right>", self.right)
        self.master.bind("<Up>", self.up)
        self.master.bind("<Down>", self.down)
 
        self.mainloop()
 
if __name__ == "__main__":
    mains()

I am pretty sure that I have made some mistakes in the mains() function as a result of which I am not getting the desired output.我很确定我在 mains() function 中犯了一些错误,因此我没有得到想要的 output。 So my question is what should I do to rectify these mistakes?所以我的问题是我应该怎么做才能纠正这些错误?

Here are the changes I made to your code:以下是我对您的代码所做的更改:

  • Removed import mainwindow from game_2048.py , as it will lead to circular import calling the functions twice.game_2048.py中删除了import mainwindow ,因为它会导致循环导入调用函数两次。

  • You created a class inheriting from a Frame , this frame is just like any other tkinter widget, requires to be placed in the window using geometric manager, like pack() / grid() / place() , you did not do that您创建了一个继承自Frame的 class ,这个框架就像任何其他 tkinter 小部件一样,需要使用几何管理器放置在 window 中,就像你没有做的那样pack() / grid() / place()

  • I also destroyed the root window before creating another new root window.在创建另一个新的root window 之前,我还销毁了root window。 Also note that instead of using Toplevel , I used Tk .另请注意,我没有使用Toplevel ,而是使用了Tk This is done, so that closing the game window will close the app.完成后,关闭游戏 window 将关闭应用程序。 But if using Toplevel , you will have to close the menu window too.但是如果使用Toplevel ,您也必须关闭菜单 window 。

from tkinter import *
import tkinter
import random
import colors as c

def mains(root):
    root.destroy()
    root = Tk()
    
    def init(root):
        l1.destroy()
        game = Game(root)
        game.pack()

    l1 = Button(root, text='Start Game',command=lambda: init(root))
    l1.pack()

class Game(tkinter.Frame):
    def __init__(self, parent):
        tkinter.Frame.__init__(self, parent)
        .......

But if you were to ask me, I would not go for such structure, but this will work here too.但是如果你要问我,我不会 go 这样的结构,但这也可以在这里工作。 Understand this, work on it, implement your own structure that does not call more than one Tk() .理解这一点,努力实现它,实现你自己的不调用多个Tk()的结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM