基本上我想在屏幕的一侧放置一个时钟,在另一侧放置文本,我使用框架。我怎样才能做到这一点。这是它现在的样子的图片:
我想让时钟与同一行上的文本一致,但有不同的标签。看看我的代码,看看你是否能以某种方式帮助我,拜托!
from tkinter import *
from tkinter import ttk
import time
root = Tk()
root.state("zoomed") #to make it full screen
root.title("Vehicle Window Fitting - Management System")
root.configure(bg="grey80")
Title = Frame(root, width=675, height=50, bd=4, relief="ridge")
Title.pack(side=TOP, anchor='w')
titleLabel = Label(Title, font=('arial', 12, 'bold'), text="Vehicle Window Fitting - Management System", bd=5, anchor='w')
titleLabel.grid(row=0, column=0)
clockFrame = Frame(root, width=675, height=50, bd=4, relief="ridge")
clockFrame.pack(side=TOP, anchor='e')
clockLabel = Label(clockFrame, font=('arial', 12, 'bold'), bd=5, anchor='e')
clockLabel.grid(row=0, column=1)
curtime = ""
def tick():
global curtime
newtime = time.strftime('%H:%M:%S')
if newtime != curtime:
curtime = newtime
clockLabel.config(text=curtime)
clockLabel.after(200, tick)
tick()
Bottom = Frame(root, width=1350, height=50, bd=4, relief="ridge")
Bottom.pack(side=TOP)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
一个问题是试图混合pack和grid布局管理器,因为它们不能很好地协同工作。下面是一些只使用pack. (请注意,您可以在一个框架内使用任何一个,但不能同时使用两者。)
为了将这两个项目放在同一“行”上,添加了另一个Frame名称topFrame,titleLabel并且clockFrame嵌套在其中的小部件和小部件。这种分组允许它们在移动或定位时作为一个单元进行操作——自动影响它们,但保持它们彼此的相对位置(LEFT和RIGHT)。
我还删除了curtime全局变量,因为它并不是真正必要的(正如您在修改后的tick()函数中看到的那样)。
from tkinter import *
from tkinter import ttk
import time
root = Tk()
root.state("zoomed") #to make it full screen
root.title("Vehicle Window Fitting - Management System")
root.configure(bg="grey80")
topFrame = Frame(root, width=1350, height=50) # Added "container" Frame.
topFrame.pack(side=TOP, fill=X, expand=1, anchor=N)
titleLabel = Label(topFrame, font=('arial', 12, 'bold'),
text="Vehicle Window Fitting - Management System",
bd=5, anchor=W)
titleLabel.pack(side=LEFT)
clockFrame = Frame(topFrame, width=100, height=50, bd=4, relief="ridge")
clockFrame.pack(side=RIGHT)
clockLabel = Label(clockFrame, font=('arial', 12, 'bold'), bd=5, anchor=E)
clockLabel.pack()
Bottom = Frame(root, width=1350, height=50, bd=4, relief="ridge")
Bottom.pack(side=BOTTOM, fill=X, expand=1, anchor=S)
def tick(curtime=''): #acts as a clock, changing the label when the time goes up
newtime = time.strftime('%H:%M:%S')
if newtime != curtime:
curtime = newtime
clockLabel.config(text=curtime)
clockLabel.after(200, tick, curtime)
tick() #start clock
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
这是运行的样子:
| 归档时间: |
|
| 查看次数: |
9367 次 |
| 最近记录: |