简体   繁体   English

麻烦搞清楚如何在tkinter中打开一个新窗口“属性错误”

[英]trouble figuring out how to open a new window in tkinter “attribute error”

I am a beginner programmer and want to open a new window after a bottom in clicked in tkinter. 我是一名初学程序员,想在tkinter点击底部之后打开一个新窗口。 I looked online and tried some things but I don't really understand it and keep getting errors. 我在网上看了一下并试了一些东西,但我并不是真的理解它并且不断出错。 Here is the error I get now. 这是我现在得到的错误。

File "C:\Python33\lib\tkinter\__init__.py", line 2046, in _setup
    self.tk = master.tk
AttributeError: 'App' object has no attribute 'tk'

Here is my code 这是我的代码

from tkinter import *
import random

player_dice = []

class App:

    def __init__(self, master):

        for i in range(1,6):
            x = random.randint(1,6)
            player_dice.append(x)
            self.label = Label(master, text = x , fg = "red").grid(row =0, column =i+1)

        self.label = Label(master, text = "Dice:" , fg = "red").grid(row =0, column =1)

        self.hi_one = Button(master, text="one", command=self.say_one).grid(row = 1, column = 1)



    def say_one(self):
        print ("1")
        window = Toplevel(self)
        self.label = Label(window, text = "you selected one" , fg = "red").grid(row =3, column =3)


root = Tk()

app = App(root)

root.mainloop()

thanks for your help 谢谢你的帮助

Pass root , not self when call Toplevel : Toplevel(root) . 调用ToplevelToplevel(root)时,传递root ,而不是self Or omit argument: Toplevel() 或省略参数: Toplevel()

from tkinter import *
import random

player_dice = []

class App:
    def __init__(self, master):
        for i in range(1,6):
            x = random.randint(1,6)
            player_dice.append(x)
            self.label = Label(master, text = x , fg = "red").grid(row =0, column =i+1)
        self.label = Label(master, text = "Dice:" , fg = "red").grid(row =0, column =1)
        self.hi_one = Button(master, text="one", command=self.say_one).grid(row = 1, column = 1)

    def say_one(self):
        print ("1")
        window = Toplevel(root) # self -> root
        self.label = Label(window, text = "you selected one" , fg = "red").grid(row =3, column =3)

root = Tk()
app = App(root)
root.mainloop()

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

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