繁体   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