Python Tkinter侧边

2024-04-16 20:46:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我不知道我的代码出了什么问题。我正在试着设置一个简单的时钟,上面写着日期,下面写着时间。如果我交换了顶框和底框的一边,它会修复它,但这对我来说没有意义。我觉得我只是错过了一些简单的事情。你知道吗

Code and output


Tags: and代码output时间code时钟事情意义
2条回答

From a website I found

side= Specifies which side to pack the widget against. To pack widgets vertically, use TOP (default). To pack widgets horizontally, use LEFT. You can also pack widgets along the BOTTOM and RIGHT edges. You can mix sides in a single geometry manager, but the results may not always be what you expect. While you can create pretty complicated layouts by nesting Frame widgets, you may prefer using the grid geometry manager for non-trivial layouts.

所以对于你的代码,我会尝试:

date.pack(side=TOP, fill=BOTH)
clock.pack(side=TOP, fill=BOTH)

即使您将TOP用于两者,它们也会按照您放置它们的顺序堆叠,因此date会放在日期的顶部,然后clock会放在日期的底部。你知道吗

编辑:另一个例子可以找到here


我认为你现在的问题是使用多帧。试试这个:

fm = Frame(root)
data = Label(fm, font=('ariel', 20, 'bold'), bg='black', fg='white')
clock = Label(fm, font=('ariel', 20, 'bold'), bg='black', fg='white')

date.pack(side=TOP, fill=BOTH)
clock.pack(side=TOP, fill=BOTH)

所以我发现了问题。由于不注意,我没有注意到问题不在包装或侧面。这是因为我用日期作为时钟变量,用时间作为日期变量。你知道吗

我用的是: 日期2=时间.strftime(“%I:%S%p”) 时间2=时间.strftime(“%A,%B%d%Y”) 而不是: 时间2=时间.strftime(“%I:%S%p”) 日期2=时间.strftime(“%A,%B%d%Y”)

不过,我很感谢大家的帮助!你知道吗

相关问题 更多 >