简体   繁体   English

标签调整大小不正确

[英]Label is resizing incorrectly tkinter

This is my first post so be easy on me :) I have a simple python 2.7 script that creates a window using overrideredirect(1) . 这是我的第一篇文章,所以对我来说很简单:)我有一个简单的python 2.7脚本,它使用overrideredirect(1)创建了一个窗口。 That all works well and good, the canvas I add works, but when I add a label and apply its height and width, it is severely elongated on both axes. 一切都很好,我添加的画布也可以工作,但是当我添加标签并应用其高度和宽度时,它在两个轴上都会严重伸长。 this occurs for both the title bar and quits labels. 标题栏和退出标签都会发生这种情况。 I want to get them thinner, but I can't use floats. 我想让它们变薄,但不能使用浮子。 Any ideas what is happening? 有什么想法吗? Here is my code: 这是我的代码:

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()

Unless your label has an image, the width and height represents a number of average sized characters based on the font used by the Label. 除非标签上有图像,否则宽度和高度将根据标签所使用的字体表示多个平均大小的字符。 A width of 700 and a height of 1 will likely result in something 6000-7000 pixels wide, and 15-20 pixels tall. 宽度700和高度1可能会导致宽度为6000-7000像素,高度为15-20像素。

If you're trying to create a border, I recommend using a frame since its width and height parameters are in pixels. 如果要创建边框,建议使用框架,因为其宽度和高度参数以像素为单位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM