Python 标签的大小调整不正确
这是我的第一篇帖子,所以请对我放松: 我有一个简单的Python2.7脚本,它使用OverrideDirect1创建一个窗口。这一切都很好,我添加的画布很好,但当我添加标签并应用其高度和宽度时,它在两个轴上都严重拉长。 标题栏和退出标签都会出现这种情况。我想让它们变薄,但我不能用浮子。知道发生了什么吗? 这是我的密码:Python 标签的大小调整不正确,python,python-2.7,tkinter,label,Python,Python 2.7,Tkinter,Label,这是我的第一篇帖子,所以请对我放松: 我有一个简单的Python2.7脚本,它使用OverrideDirect1创建一个窗口。这一切都很好,我添加的画布很好,但当我添加标签并应用其高度和宽度时,它在两个轴上都严重拉长。 标题栏和退出标签都会出现这种情况。我想让它们变薄,但我不能用浮子。知道发生了什么吗? 这是我的密码: from Tkinter import * from math import * class Main(): def __init__(self): #
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像素高
如果您试图创建边框,我建议使用边框,因为其宽度和高度参数以像素为单位。请更详细地说明问题所在,并指出涉及的代码行。更新了我的说明。我不知道还能怎么解释,但我补充了一些评论。希望它能有所帮助:|但是,当我运行代码时,它似乎工作正常,或者我不理解您的意思,它在两个轴上都被严重拉长,假设您是指窗口。