【问题标题】:Tkinter frame not displayingTkinter 框架不显示
【发布时间】:2018-08-05 07:07:13
【问题描述】:

我正在使用 TKinter 在 python 上制作这个网球 GUI 程序。 我用下面的代码得到的当前错误是'self.title_frame'甚至没有出现在第0行的GUI上。它完全消失了。是什么导致了这个问题?

此外,没有实际的错误消息。这只是顶部框架(self.title_frame)没有显示在第 0 行的问题。事实上,它不仅没有出现,而且位于第 0 行的标签(self.title_label)将显示在第 0 行但没有它下面的框架。

from tkinter import *


    #*||Variables||*

#Stylistic Variables:

active_BG = 'DarkGreen' #active background color
active_FG = 'white' #active foreground color
main_font = 'SimSun 30 bold' #main font, size and style
sub_font = 'SimSun 20 bold' #sub font, size and style
sub_font2 = 'SimSun 11 bold' #sub font 2, size and style
sub_font3 = 'SimSun 10'

bg_1 = 'MediumSeaGreen' #background color 1
bg_2 = 'palegreen' #background color 2
bg_3 = 'SeaGreen' #background color 3
fg_1 = 'DarkGreen' #foreground color 1
fg_2 = 'LightCyan' #foreground color 2
hand_cursor = 'hand2' #button hover cursor
raised_relief = 'raised' #relief style 1 (raised)
groove_relief = 'groove' #relief style 2 (groove)

#Game Variables

#STUFF...

    #*||Functions||*

class tenalyzer_V1:

    def __init__(self, root):
        self.root = root

        self.title_frame = Frame(self.root)
        self.title_frame.grid(row=0)
        self.title_frame.config(bg=bg_1, width=520, height=100)

        self.title_label = Label(self.title_frame)
        self.title_label.grid(row=0)
        self.title_label.config(text='Tenalyzer v1.0', bg=bg_1, fg=fg_2, font=main_font, width='17', bd=10, relief=groove_relief)

        self.start_frame = Frame(self.root, bg=bg_3, width=520, height=100).grid(row=1)
        self.start_label = Button(self.start_frame)
        self.start_label.grid(row=1)
        self.start_label.config(text='START', bg=bg_2, activebackground=active_BG , fg=fg_1, activeforeground=active_FG, font=sub_font, width=14, bd=5, relief=raised_relief, cursor=hand_cursor, command=self.start_page)

        self.instructions_frame = Frame(self.root, bg=bg_1, width=520, height=100).grid(row=2)
        self.instructions_btn = Button(self.instructions_frame)
        self.instructions_btn.grid(row=2)
        self.instructions_btn.config(text='INSTRUCTIONS', bg=bg_2, activebackground=active_BG , fg=fg_1, activeforeground=active_FG, font=sub_font, width=14, bd=5, relief=raised_relief, cursor=hand_cursor, command=self.instructions_page)

        self.credit_frame = Frame(self.root, bg=bg_3, width=520, height=100).grid(row=3)
        self.credit_btn = Button(self.credit_frame)
        self.credit_btn.grid(row=3)
        self.credit_btn.config(text='CREDITS', bg=bg_2, activebackground=active_BG , fg=fg_1, activeforeground=active_FG, font=sub_font, width=14, bd=5, relief=raised_relief, cursor=hand_cursor, command=self.credits_page)

        self.return_frame = Frame(self.root, bg=bg_1, width=520, height=40).grid(row=4)
        self.return_btn = Button(self.return_frame)
        self.return_btn.grid(row=4)
        self.return_btn.config(text='Exit', bg=bg_2, activebackground=active_BG , fg=fg_1, activeforeground=active_FG, font=sub_font2, width=16, bd=5, relief=raised_relief, cursor=hand_cursor, command=self.exit_command)

    def exit_command(self):
        root.destroy()

    def title_page(self):
        self.title_label.grid(row=0)
        self.start_label.grid(row=1)
        self.instructions_btn.grid(row=2)
        self.credit_btn.grid(row=3)

        self.title_label.config(text='Tenalyzer v1.0')
        self.return_btn.config(text='Exit', command=self.exit_command)

    def start_page(self):
        print('started')

    def instructions_page(self):
        print('instructions page')

    def credits_page(self):
        print('credits page')
        self.title_label.config(text='Credits:')
        self.return_btn.config(text='Go Back', command=self.title_page)

    #*||Main Program||*

root = Tk()
root.title('Tenalyzer v1.0')
root.resizable(width=FALSE, height=FALSE)
root.geometry('{}x{}'.format(520, 440))
keyword = tenalyzer_V1(root)
root.mainloop()

【问题讨论】:

标签: python tkinter configuration grid nonetype


【解决方案1】:

框架正在显示,因为标签在框架内,所以如果你能看到,框架已经被打包。我认为您遇到的问题是框架不符合您为其指定的宽度和高度。 要解决此问题,请关闭网格传播。即。

self.root.grid_propagate(False)

这意味着当一个小部件被添加到框架时,框架不会调整大小以匹配小部件。

有关更多信息,请参阅 Bryan Oakley 对此问题的回答: How to set the min and max height or width of a Frame?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-21
    • 2020-09-15
    • 1970-01-01
    • 2022-11-29
    • 2019-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多