标签调整错误tkinter

问题描述 投票:0回答:1

这是我的第一篇文章,所以对我很容易:)我有一个简单的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()
python python-2.7 tkinter label
1个回答
1
投票

除非您的标签有图像,否则宽度和高度表示基于Label使用的字体的多个平均大小的字符。宽度为700,高度为1可能会产生6000-7000像素宽,15-20像素高。

如果您正在尝试创建边框,我建议使用框架,因为其宽度和高度参数以像素为单位。

© www.soinside.com 2019 - 2024. All rights reserved.