- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 Python 和 Tkinter 的新手。
我已经创建了我的界面,但 .grid 似乎没有按照我的要求执行。LANframe
不是在中心框架中看起来整洁,而是 float 在中心而不是靠近测验。
此外,统计数据不想填充 Factframe
中剩余的青色空间,因此看起来很奇怪。
我该如何整理它?
The main outlines work fine but when I Try to put 3 additional frames into those3 main frames, they either overlap each other or they are in randomplaces like show in the photo
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]: /image/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"
或 side="right"
.
这就是.pack
作品:
----------
| |
| widget 1 |
| |
----------
如果您调用 <widget 2>.pack(side="right")
然后新的小部件将像这样放在最后一个 (widget 1
) 的右边:
---------- ----------
| | |
| widget 1 | widget 2 |
| | |
---------- ----------
关于框架内的python tkinter框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67142491/
在 How to make a Tkinter window jump to the front? 中提出的问题之后的一个问题 我希望有一个顶层窗口(我用它来导航我的其他主窗口)总是在前面。但我希望它
有没有办法在 Tkinter 中保持小部件(特别是图像)的堆叠顺序一致?例如,我可能在 Canvas 上的同一位置有两个矩形、两个三角形和一个圆。圆圈移动到最后一次点击鼠标的地方,但我总是希望它被绘制
这是一个简单的 GUI 程序,用于创建 5x16 按钮矩阵。 from tkinter import * root = Tk() button = [[0 for x in range(16)] fo
有一个错误:“AttributeError: module 'tkinter' has no attribute 'messagebox'” 即使 import tkinter 一开始就已经给出了,为
我知道 menu.tk_popup() 可用于在特定坐标处打开上下文菜单,但也不知道如何从中打开子菜单,如果这有意义的话。这是我编写的代码: import tkinter as tk root = t
我正在尝试在禁用自动换行和水平滚动条的文本窗口中书写,如下所示: root = Toplevel() root.geometry("%dx%d+0+0" % (350,400)) af=Frame(r
已经将文本变量分配给小部件后,如何将其删除? widget.config(textvariable=None)只是不工作。在谷歌或这里找不到任何东西。 最佳答案 将您的变量分配给一个空字符串以实现此目
Jython 支持 Tkinter 吗?如果我用 Python 编写一个程序并放一个 使用 Tkinter 的 GUI 前端,做同样的事情有多难 Jython 中的程序?或者对于 Jython GUI
因此,我尝试创建一个 tkinter 窗口,显示当前时间和日期以及自定义短语。不过,我遇到的问题是,我似乎无法在第二天刷新日期。 我可以传递截至运行代码时的当前日期,但之后它变为静态。 这是我目前的程
我的理解是在初始化 __init__ 中的所有框架和小部件之后方法,tkinter 窗口会调整大小以适合所有这些组件。 我想将窗口的初始化大小设置为其最小大小。我希望能够最大化并放大窗口,但我从不希望
此代码仅水平居中,如何使进度条也垂直居中? import Tkinter import ttk root = Tkinter.Tk() root.geometry("=500x500") root.p
使用 Python 2.7 和 Tkinter 模块,我创建了一个菜单按钮并为其分配了一个菜单。现在每次我在特定位置发布菜单时,菜单的宽度都会根据字符数自动设置。有没有办法在菜单小部件中设置静态宽度?
我想将我的 tkinter 应用程序的主题更改为 clam。 代码是什么,我把它放在哪里?我试过了: from tkinter import * from tkinter.ttk import * s
我有以下代码: from Tkinter import * from urllib import urlretrieve import webbrowser import ttk def get_la
我知道,如果我将滚动条控制的框架绑定(bind)到函数 ( onFrameConfigure ),您可以获得滚动条位置,如下所示:self.calendar_frame.bind("", self.o
许多网站都说菜单小部件有一个选项“字体”,但我一直无法设置它。系统是在 Windows 8.1 中运行的 Python 3.5。脚本开始: 从 tkinter 导入 * 根 = Tk() root.g
我正在阅读本教程,它帮助我同时学习 tkinter 和 wxWidgets,但我想深入挖掘,所以想知道哪个 GUI 工具更适合深入学习,为什么? 最佳答案 不可能说哪个“更好”。两者均可用于最常见的用
看书学python,tkinter.END用在一段代码里不用解释 import tkinter def count(text, out_data): """ Update out_data w
我正在尝试使用 Python 2.7 将 Tkinter 导入到我的项目中,但我收到了错误: ImportError: No module named tkinter 在有人说之前,我已经尝试了“Tk
当我回答 Tkinter 问题时,我通常会尝试自己运行代码,但有时我会收到此错误: Traceback (most recent call last): File "C:\Python27\pyg
我是一名优秀的程序员,十分优秀!