0

I am a beginner programmer and want to open a new window after a bottom in clicked in 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

4

1 回答 1

2

通过rootself不当调用ToplevelToplevel(root)。或省略参数: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()
于 2013-07-25T07:19:44.797 回答