【问题标题】:Buttons/Lables are not showing up on the new window (Python tkinter)按钮/标签未显示在新窗口上(Python tkinter)
【发布时间】:2018-08-11 17:18:34
【问题描述】:

当我单击井字按钮时,它应该会启动一个新窗口,但它不会显示按钮,也不会出现任何错误。

这两段代码在两个不同的脚本中。

当我在自己的脚本中运行井字游戏时,它会运行并显示所有按钮。

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()

这是井字游戏的 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()

【问题讨论】:

    标签: python-3.x tkinter


    【解决方案1】:

    问题是按钮和标签没有master 参数。 master 指的是小部件将在其中显示的父窗口。

    例如。第一个按钮:

    self.c1r1=tk.Button(text='',width=w, height=h, ...)
    

    您已经按照name, widget type, (options...) 的格式定义了它。 名称为c1r1,小部件类型为tk.Button,选项为text, width, height 等...

    但是,小部件的正确格式是 name, widget type (master, options...)

    由于按钮的父窗口定义为“self.home=tk.Tk()”,因此需要引用此父窗口来代替该窗口中定义的任何小部件的master 参数。

    因此,考虑到这一点,您的第一个按钮现在将定义为:

    self.c1r1=tk.Button(self.home, text='',width=w, height=h, ...)
    

    确保将self.home 添加到其他按钮,它们会显示出来。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-02
      • 2017-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多