Tkinter标签尺寸不匹配

时间:2016-03-30 12:52:35

标签: python tkinter

我正在研究人机界面设计...... 我创建了一个标签数组......我为数组中的所有标签使用了相同的标签和属性,但是当我执行代码时,标签的大小彼此不匹配....

这是我的代码......

from tkinter import *    
import time

root = Tk()

class Clock:
    def __init__(self):
        self.time1 = ''
        self.time2 = time.strftime('%H:%M:%S')
        self.mFrame = Frame()
        self.mFrame.pack(side=TOP,fill=X)

        self.bottomFrame = Frame()
        self.bottomFrame.pack(side=BOTTOM,fill=BOTH)

        root.title('HUMAN MACHINE INTERFACE')

        self.company_name=Label(self.mFrame, text='Host Company Name', font=('Arial',24,'bold','underline'),bg='white',relief=RAISED).pack(fill=X)

        self.machine_name=Label(self.mFrame, text='Machine name', font=('Arial',24,'bold','underline'),bg='yellow',relief=RAISED).pack(fill=X)

        self.my_company_name=Label(self.mFrame, text='SYNERGY AUTOMATION', font=('Arial',24,'bold','underline'),relief=SUNKEN).pack(side=LEFT)

        self.watch = Label(self.mFrame, text=self.time2, font=('times',24,'bold'),relief=SUNKEN)
        self.watch.pack(side=RIGHT)

        self.clock() #first call it manually

        num=['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',]
        r=0
        c=0

        for i in num:
            Label(self.bottomFrame, text=i,bg='red',fg='white',font=(34),padx=50,pady=20,relief=SUNKEN).grid(row=r,column=c)
            c = c + 1
            if c == 4 or c == 8:
                c=0
                r=r+1




    def clock(self): 
        self.time2 = time.strftime('%H:%M:%S')
        self.watch.configure(text=self.time2)
        self.mFrame.after(200, self.clock) #it'll call itself continuously

obj1 = Clock()
root.mainloop()

1 个答案:

答案 0 :(得分:2)

致电sticky时,您未使用grid选项。没有它,标签只会大到足以包含文本。如果您希望它们填充表格单元格,请使用sticky=N+S+E+W

之类的内容
Label(...).grid(... sticky=N+S+E+W)

顺便说一下,

self.company_name=Label(...).pack(fill=X)

您将self.company_name设置为None,因为pack返回的是self.company_name=Label(...) ... self.company_name.pack(fill=X) 。 tkinter最佳做法是将窗口小部件的创建与在屏幕上放置它分开:

<Window x:Class="TestThis.MainWindow"
<-- Just deleted some code here to make it look better -->  
    xmlns:local="clr-namespace:TestThis"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525"
    d:DataContext="{d:DesignInstance local:Presenter}">
<StackPanel>
    <ListBox ItemsSource="{Binding Strings}"></ListBox>
</StackPanel>
</Window>