【问题标题】:Why the text is not displayed on the label?为什么文字不显示在标签上?
【发布时间】:2023-03-10 05:05:01
【问题描述】:

如果我手动放大窗口,我会看到文字,所以文字就在那里! (在标签上)但是如果不放大我看不到的窗口,我只能看到标签的一部分

我该如何解决?我必须调整大小吗?

我尝试过更改字体大小、标签大小和其他一些东西,但我什至无法解决问题。

这是代码:

from Tkinter import *
import ttk
import time
import threading

def start():
    start_stop.config(text="QUIT", command=stop, image=photo2)
    thread = threading.Thread(target=progBar, args=())
    thread.daemon = True
    thread.start()

def progBar():
    for i in xrange(300):
        if i < 50:
            lbl1.config(height=0, width=4, font=('times', 400, 'bold'),
                        text="AAAA!!")     
            lbl1.config(fg='black')

            if i % 5:
                lbl1.config(bg='white')
            else:
                lbl1.config(bg='red')

        if i == 50:
            lbl1.destroy()

        if i >= 200:
            s = ttk.Style()
            s.theme_use('clam')
            if i % 5:
                s.configure("red.Horizontal.TProgressbar", 
                            foreground='#205F8C', background='#205F8C')
            else:
                s.configure("red.Horizontal.TProgressbar", 
                            foreground='red', background='red')
            pbar_det.config(style="red.Horizontal.TProgressbar")

        pbar_det.step(0.33)
        master.update()
        # Busy-wait
        time.sleep(0.1)

    master.destroy()

def stop():
    master.destroy()

master = Tk()
photo1 = PhotoImage(file="Press1.gif")
photo2 = PhotoImage(file="Press2.gif")
ws = master.winfo_screenwidth() # width of the screen
hs = master.winfo_screenheight() # height of the screen

width = ws
height = 120
x = ws-width # Window's coords
y = hs-height

start_stop = Button(master, text='START', command=start, image=photo1)
start_stop.grid(row=0, column=1, pady=2, padx=2, sticky=E+W+N+S)

pbar_det = ttk.Progressbar(master, orient="horizontal", length=ws-450,
                           mode="determinate")
pbar_det.grid(row=0, column=0, pady=2, padx=2, sticky=E+W+N+S)

lbl1 = Label(master)
lbl1.grid(row=0, column=1, pady=2, padx=2)
lbl2 = Label(master)
lbl2.grid(row=0, column=1, pady=2, padx=2)

master.geometry('%dx%d+%d+%d' % (width, height, x, y))
master.mainloop()

【问题讨论】:

  • 你做了什么来调试这个?您是否验证了您对宽度和高度的计算是否符合您的预期?另外,您是否有意将两个按钮和两个图像放在同一个位置(第 0 行,第 1 列)?

标签: python tkinter label


【解决方案1】:

尝试将标签内的文本锚定到左侧。

lbl1.config(anchor="nw") # nw = north-west = top left

【讨论】:

  • 感谢您的回答!但这并不能解决问题。问题完全一样
【解决方案2】:

我看到代码中至少有四个问题:

  • 当您调用master.geometry(...) 时,您将小部件的大小强制为特定高度。这可以防止窗口增长或缩小以适应内部小部件
  • 您指定的字体非常大(400 磅),无法适应您选择的高度。由于相对于您给它的空间(通过显式设置窗口的高度)字体的巨大尺寸,您看到的是所有字体中存在的字母上方的自然空白)。
  • 您将两个标签和一个按钮放在同一行和同一列中
  • 您正在使用线程。 Tkinter 不是线程安全的。您的代码可能有效,但可能无效。

如果您删除对master.geometry() 的调用,窗口将展开以适合文本的高度。或者,你不能把字体做得那么大。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-15
    相关资源
    最近更新 更多