python - 框架内的python tkinter框架
问题描述
我是 Python 和 Tkinter 的新手。
我已经创建了我的界面,但 .grid 似乎没有做我想做的事。
在中心框架中没有整洁的外观,LANframe
而是漂浮在中心而不是靠近测验。
此外,统计数据不想填充剩余的青色空间,Factframe
因此看起来很奇怪。
我怎样才能整理这个?
主要轮廓工作正常,但是当我尝试将 3 个附加帧放入这 3 个主要帧中时,它们要么相互重叠,要么位于随机位置,如照片中所示
login= Tk()
screen_width = login.winfo_screenwidth()
screen_height = login.winfo_screenheight()
screen_height=str(screen_height)
screen_width=str(screen_width)
screen = screen_width+"x"+screen_height
login.geometry(screen)
login.title("Logged in as %s"%name)
login.iconbitmap('icons/mainicon.ico')
global min_w, max_w, cur_width, expanded
min_w = 150 # Minimum width of the frame
max_w = 300 # Maximum width of the frame
cur_width = min_w # Increasing width of the frame
expanded = False # Check if it is completely exanded
# Define the icons to be shown
global exits, settings, feed, profile
exits = PhotoImage(file='icons/exits.png') # Make sure the image size is comparable to the minimum frame width
settings = PhotoImage(file='icons/settings.png')
feed = PhotoImage(file='icons/feed.png')
profile = PhotoImage(file='icons/profile.png')
login.update() # For the width to get updated
global width1
width1 = (int(screen_width)-200)
global frame
login.grid_rowconfigure(1, weight=1)
login.grid_columnconfigure(0, weight=1)
frame = Frame(login,bg=barcolour,width=120,height=login.winfo_height())
frame.grid(row=0,column=0,sticky="nsw")
global frame1
frame1 = Frame(login,bg="blue",width=width1,height=login.winfo_height())
frame1.grid(row=0,column=5, sticky = "e")
frame1.grid_rowconfigure(0, weight=1)
frame1.grid_columnconfigure(1, weight=1)
width2 = (width1/3)
global friendframe
friendframe = Frame(frame1,bg="blue",width=width2,height=login.winfo_height())
friendframe.grid(row=0,column=1,sticky="e")
centreframe = Frame(frame1,bg="red",width=width2,height=login.winfo_height())
centreframe.grid(row=0,column=2,sticky="nsew")
centreframe.grid_rowconfigure(1, weight=1)
centreframe.grid_columnconfigure(0, weight=1)
factframe= Frame(frame1,bg="cyan",width=width2,height=login.winfo_height())
factframe.grid(row=0,column=3,sticky="ns")
timeheight=round(int(login.winfo_height())/5)
timeframe = Frame(centreframe)
timeframe.grid(row=0,sticky="n", padx=3,pady=3,columnspan=3)
global clocklabel
clocklabel = Label(timeframe,bg =backgrounde,font="Ariel 40")
clocklabel.grid(row=1,sticky="nesw")
display_time()
quizheight= round(int(login.winfo_height())/4)
quizgoto= partial(gotoquizinterface)
quizicons = PhotoImage(file='icons/quizicon.png')
quizbutton = Button(centreframe,bg="green",image=quizicons,text=" \n Quizes",compound="left",font="Ariel 40",command=quizgoto)
quizbutton.grid(row=1,sticky="new",padx=3,pady=3,columnspan=3)
LanFrame= Frame(centreframe,height=quizheight)
LanFrame.grid(row=1,padx=3,pady=3,)
Langameplayicon= PhotoImage(file='icons/LANgameplay.png')
LANLobby = partial(LANGameplay, name)
recentachieve = "Under construction"
LANButton = Button(LanFrame,image =Langameplayicon, bg="yellow", text=" \n LAN Gameplay \n Under Construction",compound="left",font="Ariel 30",command=LANLobby)
LANButton.grid(sticky="new")
Achievementsicon=PhotoImage(file='icons/Awards.png')
Achievements = partial(achievementsgained, name)
AchievementsButton = Button(factframe,image =Achievementsicon, bg="purple", text=(" \n Achievements \n Recent Achievements: \n"+recentachieve+"\n"),compound="left",font="Ariel 30",command=Achievements)
AchievementsButton.grid(sticky="new")
Statsicon = PhotoImage(file='icons/Statistics.png')
statsframe = Frame(factframe,bg="pink",height=login.winfo_height())
statsframe.grid(row=1,sticky="esw")
Showstats = partial(statshownow, name)
timeon = "0"
quizdone = "0"
Avgscore = "0"
statsbuttons= Button(statsframe, image =Statsicon, text =(" Stats: \n Time spent on:"+timeon+"\n Quizes Done:"+quizdone+"\n Average Score:"+Avgscore+"\n"),compound="left",font="Ariel 20",command=Showstats)
statsbuttons.grid(sticky="nesw",columnspan=4,rowspan=5)
global profile_b, exits_b, set_b, feed_b
profilelabel = Label(frame,text=' ',bg=barcolour).grid(row=0,column=1)
exitslabel = Label(frame,text=' ',bg=barcolour).grid(row=1,column=1)
setlabel = Label(frame,text=' ',bg=barcolour).grid(row=2,column=1)
feedlabel = Label(frame,text=' ',bg=barcolour).grid(row=3,column=1)
profile_b = Label(frame,text=' ',image=profile,compound="right",bg=barcolour,relief=RIDGE)
exits_b = Button(frame,text=' ',image=exits,compound="right",bg=barcolour,relief=RAISED)
global set_b
set_b = Button(frame,text=' ',image=settings,compound="right",bg=barcolour,relief=GROOVE)
feed_b = Button(frame,text=' ',image=feed,compound="right",bg=barcolour,relief=SUNKEN)
profile_b.grid(row=0,column=3,pady=10, columnspan=2)
exits_b.grid(row=1,column=3,pady=10, columnspan=2)
set_b.grid(row=2,column=3,pady=50, columnspan=2)
feed_b.grid(row=3,column=3, columnspan=2)
frame.bind('<Enter>',lambda e: expand())
frame.bind('<Leave>',lambda e: contract())
frame.grid_propagate(False)
login.mainloop()
[不需要的结果] [1]:https ://i.stack.imgur.com/UBA0N.png
解决方案
尝试这个:
import tkinter as tk
root = tk.Tk()
root.geometry("500x500")
# Create the left frame
left_frame = tk.Frame(root, bg="blue")
left_frame.pack(side="left", fill="both", expand=True)
# Create the center frame
center_frame = tk.Frame(root, bg="red")
center_frame.pack(side="left", fill="both", expand=True)
# Populate the center frame
time_frame = tk.Frame(center_frame, bg="white")
time_frame.pack(side="top", fill="x")
# Show the time+date
time_label = tk.Label(time_frame, text="20:44:22", bg="white")
time_label.pack(side="top", fill="x")
date_label = tk.Label(time_frame, text="17/4/2021", bg="white")
date_label.pack(side="top", fill="x")
# Show the `Quizes` button
quizes_frame = tk.Frame(center_frame, bg="green")
quizes_frame.pack(side="top", fill="x")
# Show the image on the left and the label on the right
quizes_img = tk.Label(quizes_frame, bg="green", image=None)
quizes_img.pack(side="top", fill="x", expand=True)
quizes_label = tk.Label(quizes_frame, text="Quizes", bg="green")
quizes_label.pack(side="top", fill="x", expand=True)
# Show the `Lan` button
lan_frame = tk.Frame(center_frame, bg="yellow", height=100) # Dummy height
lan_frame.pack(side="top", fill="x")
# Create the right frame
right_frame = tk.Frame(root, bg="pink")
right_frame.pack(side="left", fill="both", expand=True)
我重写了您的部分代码并使用了,.pack
因为大多数时候它是side="top"
or side="right"
。
这是如何.pack
工作的:
- 您可以指定一个方向,它将根据最后创建的小部件放置在该方向上。想象一下这种情况:
----------
| |
| widget 1 |
| |
----------
如果您调用<widget 2>.pack(side="right")
,那么新的小部件将转到最后一个放置 ( widget 1
) 的右侧,如下所示:
---------- ----------
| | |
| widget 1 | widget 2 |
| | |
---------- ----------
推荐阅读
- dart - Java Integer.parseInt 与 Dart int.parse
- python - 请求:过滤 r.json() 结果
- haskell - Haskell,返回第 n 次
- excel - 无法引用工作表行。只返回当前工作表的行数据
- highcharts - Highcharts dataLabels 阴影
- pymodbus - 如何在pymodbus中实现带有自定义功能代码的消息?
- android - 平板电脑上的程序化子菜单在不同的Android版本中工作方式不同,但对手机没有区别?
- mysql - JavaFX tableview 列切换值
- mysql - mysql从百万条记录中获取列连接的平均值
- java - 将我的应用程序置于 Android 后台后如何恢复以前的应用程序