[英]Label text isn't appearing in tkinter Frame
I've been researching this question, but none of the solutions I've found seem to work.我一直在研究这个问题,但我发现的所有解决方案似乎都不起作用。 I'm trying to get a
Label
( self.status_bar
in the code below) to appear in my frame, but any edits (ie using update()
method or resizing the frame/text widget/window) I've made have gotten me nowhere.我试图让一个
Label
(下面代码中的self.status_bar
)出现在我的框架中,但是我所做的任何编辑(即使用update()
方法或调整框架/文本小部件/窗口的大小)都让我无处。
from tkinter import *
from tkinter import filedialog
from tkinter import font
#Build frame with features to put into parent window
class TextEditor:
def __init__(self, master):
self.master = master
self.frame = tk.Frame(self.master)
#Create Scrollbar
self.text_scroll = tk.Scrollbar(self.frame)
self.text_scroll.pack(side=RIGHT, fill=Y)
#Create text box
self.text = tk.Text(self.frame, width=155, height=55, font=('Helvetica', 12), selectbackground="yellow",
selectforeground = "black", undo=True, yscrollcommand=self.text_scroll.set)
self.text.pack()
#Configure scrollbar
self.text_scroll.config(command=self.text.yview)
#Create menu
self.menu = tk.Menu(self.master)
self.master.config(menu=self.menu)
#Add file menu
self.file_menu = tk.Menu(self.menu, tearoff=False)
self.menu.add_cascade(label="File", menu=self.file_menu)
self.file_menu.add_command(label="Open")
self.file_menu.add_command(label="Save")
self.file_menu.add_command(label="New")
self.file_menu.add_separator()
self.file_menu.add_command(label="Exit", command=self.master.destroy)
#Add edit menu
self.edit_menu = tk.Menu(self.menu, tearoff=False)
self.menu.add_cascade(label="Edit", menu=self.edit_menu)
self.edit_menu.add_command(label="Cut")
self.edit_menu.add_command(label="Copy")
self.edit_menu.add_command(label="Undo")
self.edit_menu.add_command(label="Redo")
#Add status bar to bottom of app
self.status_bar = tk.Label(self.frame, text="Ready", anchor=E)
self.status_bar.pack(fill=X, side=BOTTOM, ipady=5)
#Pack frame into window
self.frame.pack()
#Instantiates the text editor app
def main():
root = tk.Tk()
app = TextEditor(root)
root.geometry("1220x660")
root.title("Text Editor")
root.mainloop()
if __name__ == '__main__':
main()
You are forcing the window to a size that is too small to fit all of the widgets.您正在强制窗口的大小太小而无法容纳所有小部件。 When you do that while using
pack
, pack
will start to shrink widgets in order to make them fit, starting with the last widget that was packed.当您在使用
pack
时这样做时, pack
将开始缩小小部件以使其适合,从最后一个被打包的小部件开始。 In this case that's the status bar.在这种情况下,这是状态栏。 So,
pack
starts removing pixels from self.status_bar
until there's enough room for the other widgets.因此,
pack
开始从self.status_bar
删除像素,直到有足够的空间容纳其他小部件。 Eventually, it has to completely remove the status bar, and then start shrinking the text widget.最终,它必须完全删除状态栏,然后开始缩小文本小部件。
The first step is to create the status bar first, so that the text widget is higher in the stacking order (ie: pack
will try to shrink it before shrinking other widgets).第一步是先创建状态栏,使文本小部件在堆叠顺序中较高(即:
pack
会在缩小其他小部件之前尝试缩小它)。
The second thing you should do is use the appropriate options to get the TextEditor
window to fill the frame, and get the frame to fill the window.您应该做的第二件事是使用适当的选项让
TextEditor
窗口填充框架,并使用框架填充窗口。 For example:例如:
self.text.pack(fill="both", expand=True)
self.frame.pack(fill="both", expand=True)
I suggest, with the more complicated layout you have, that you use the grid
method instead of pack
.我建议,对于更复杂的布局,您使用
grid
方法而不是pack
。 Here is the code with the widgets gridded instead of packed:这是小部件网格化而不是打包的代码:
import tkinter as tk
from tkinter import filedialog
from tkinter import font
from tkinter.constants import *
#Build frame with features to put into parent window
class TextEditor:
def __init__(self, master):
self.master = master
self.frame = tk.Frame(self.master)
#Create Scrollbar
self.text_scroll = tk.Scrollbar(self.frame)
self.text_scroll.grid(row=0, column=1, sticky=E+NS) ### EDITED THIS LINE
#Create text box
self.text = tk.Text(self.frame, font=('Helvetica', 12), selectbackground="yellow", ### EDITED THIS LINE
selectforeground = "black", undo=True, yscrollcommand=self.text_scroll.set)
self.text.grid(row=0, column=0, sticky=NSEW) ### EDITED THIS LINE
#Configure scrollbar
self.text_scroll.config(command=self.text.yview)
#Create menu
self.menu = tk.Menu(self.master)
self.master.config(menu=self.menu)
#Add file menu
self.file_menu = tk.Menu(self.menu, tearoff=False)
self.menu.add_cascade(label="File", menu=self.file_menu)
self.file_menu.add_command(label="Open")
self.file_menu.add_command(label="Save")
self.file_menu.add_command(label="New")
self.file_menu.add_separator()
self.file_menu.add_command(label="Exit", command=self.master.destroy)
#Add edit menu
self.edit_menu = tk.Menu(self.menu, tearoff=False)
self.menu.add_cascade(label="Edit", menu=self.edit_menu)
self.edit_menu.add_command(label="Cut")
self.edit_menu.add_command(label="Copy")
self.edit_menu.add_command(label="Undo")
self.edit_menu.add_command(label="Redo")
#Add status bar to bottom of app
self.status_bar = tk.Label(self.frame, text="Ready", anchor=E)
self.status_bar.grid(row=1, column=0, sticky=S+EW) ### EDITED THIS LINE
# Configure the rows and columns so that they expand properly ### ADDED THESE LINES
self.frame.rowconfigure(0, weight=1) ### ADDED THESE LINES
self.frame.columnconfigure(0, weight=1) ### ADDED THESE LINES
#Pack frame into window
self.frame.pack(expand=YES, fill=BOTH) ### EDITED THIS LINE
#Instantiates the text editor app
def main():
root = tk.Tk()
app = TextEditor(root)
root.geometry("1220x660")
root.title("Text Editor")
root.mainloop()
if __name__ == '__main__':
main()
Notice how I also changed the line where the frame is packed into the window.请注意我还更改了将框架打包到窗口中的行。 The only thing keeping the frame filling the window before was the size of the text widget.
之前保持框架填充窗口的唯一因素是文本小部件的大小。
With these changes, the widgets expand properly, so I also removed the width
and height
parameters from the creation of self.text
.通过这些更改,小部件可以正确扩展,因此我还从
self.text
的创建中删除了width
和height
参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.