簡體   English   中英

標簽調整大小不正確

[英]Label is resizing incorrectly tkinter

這是我的第一篇文章,所以對我來說很簡單:)我有一個簡單的python 2.7腳本,它使用overrideredirect(1)創建了一個窗口。 一切都很好,我添加的畫布也可以工作,但是當我添加標簽並應用其高度和寬度時,它在兩個軸上都會嚴重伸長。 標題欄和退出標簽都會發生這種情況。 我想讓它們變薄,但不能使用浮子。 有什么想法嗎? 這是我的代碼:

from Tkinter import *
from math import *

class Main():
    def __init__(self):
        # Create a basic tkinter window
        self.root = Tk()
        self.canvas = Canvas(self.root, width=700, height=500)
        self.canvas.pack()
        # Start function that creates labels
        self.initiate()
        # Make the window frameless
        self.root.overrideredirect(1)
        self.root.mainloop()

    def initiate(self):
        # Create a single black bar across the top of the window
        # My issue is here. I apply the height of 1, and it displays in the tkinter window of a height more like 10-20.
        # I think it has somthing to do with overrideredirect, but i dont know what it is, i have never used it before.
        self.titlebar = Label(self.canvas, width=700, height=1,
            bg='black')
        self.titlebar.place(x=0, y=0)
        # This and the other functions make the window move when you drag the titlebar. It is a replacement for the normal title bar i removed with overrideredirect.
        self.titlebar.bind("<ButtonPress-1>", self.StartMove)
        self.titlebar.bind("<ButtonRelease-1>", self.StopMove)
        self.titlebar.bind("<B1-Motion>", self.OnMotion)

        # Create a quit button in top left corner of window
        self.quit = Label(self.canvas, width=1, height=1, bg='grey')
        self.quit.place(x=0, y=0)
        self.quit.bind("<Button-1>", lambda event: self.root.destroy())

    def StartMove(self, event):
        self.x = event.x
        self.y = event.y

    def StopMove(self, event):
        self.x = None
        self.y = None

    def OnMotion(self, event):
        deltax = event.x - self.x
        deltay = event.y - self.y
        x = self.root.winfo_x() + deltax
        y = self.root.winfo_y() + deltay
        self.root.geometry("+%s+%s" % (x, y))


Main()

除非標簽上有圖像,否則寬度和高度將根據標簽所使用的字體表示多個平均大小的字符。 寬度700和高度1可能會導致寬度為6000-7000像素,高度為15-20像素。

如果要創建邊框,建議使用框架,因為其寬度和高度參數以像素為單位。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM