from tkinter import *
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
frtop=self.winfo_toplevel() #Flexible Toplevel of the window
screen_width = self.winfo_screenwidth()
screen_height = self.winfo_screenheight()
print("width="+str(screen_width)+", height="+str(screen_height))
max_windows_size=str(screen_width)+"x"+str(screen_height)
frtop.geometry(max_windows_size)
self.grid()
self.table_frame = Frame(self)
self.table_frame.grid(column =0, row =0,sticky='NSEW')
self.table_headers_title = ['','Description','Quantity','Rate', 'Amt','pending']
self.total_columns = len(self.table_headers_title)
self.table_headers=[]
for j in range(self.total_columns):
self.data_tbl = Entry(self.table_frame, fg='blue', font=('Arial',16,'bold'))
self.table_headers.append(self.data_tbl)
self.table_frame.grid_columnconfigure(j, weight=1)
self.data_tbl.grid(row=0, column=j)
self.data_tbl.insert(0, self.table_headers_title[j])
if __name__ == "__main__":
global app
app = Application()
app.master.title("Sample application")
app.mainloop()
我希望所有条目都具有相同的宽度并完全占据框架。
它没有发生。实际上,最后一列是“pendi”。
我做错了什么?
谢谢。
我相信问题是您在应用程序中使用
Frame
而不是 Tk
。问题不在于您不能使用 Frame
,问题在于它让人感到困惑,并迫使您做一些奇怪的事情来与主应用程序窗口对话。如果您只是使用应用程序小部件的主应用程序窗口,那就更直接了。
我重构了脚本中的所有噪音,更改了没有意义的内容(例如使用标题的条目),并在我进行更改的地方留下了评论面包屑 - 以突出显示它们。
# import tkinter as alias, not *
import tkinter as tk
# use Tk instead of Frame
class Application(tk.Tk):
def __init__(self, title:str="Sample Application", width:int=0, height:int=0):
tk.Tk.__init__(self)
self.title(title)
self.table_frame = tk.Frame(self)
self.table_frame.grid(column=0, row=0, sticky='nswe')
self.table_headers=[]
lbl_cfg = {
'foreground' : 'blue',
'font' : 'Arial 16 bold',
'padx' : 20,
'pady' : 4,
'borderwidth': 1,
'relief' : 'raised'
}
for j, title in enumerate(('', 'Description', 'Quantity', 'Rate', 'Amt', 'pending')):
self.table_frame.grid_columnconfigure(j, weight=1)
# Label makes more sense than Entry
header = tk.Label(self.table_frame, text=title, **lbl_cfg)
header.grid(row=0, column=j)
self.table_headers.append(header)
# update so we can get the current dimensions
self.update_idletasks()
# set geometry based on current dimension
width = width or self.table_frame.winfo_width()
height = height or self.winfo_screenheight()
self.geometry(f'{width}x{height}')
if __name__ == "__main__":
Application(height=800).mainloop()