【发布时间】:2021-09-05 19:25:16
【问题描述】:
我正在尝试将我的程序窗口居中并四处寻找答案,其中大多数都相似,例如 a/2 - b/2。
我尝试了解决方案,但得到了一个错误的回报。一开始我以为是因为我用的是f-string,所以我换掉了%-formatting,结果还是一样的错误。
例如:
import tkinter as tk
root.title("Testing")
window_width = 400
window_height = 200
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x_coordinate = (screen_width/2) - (window_width/2)
y_coordinate = (screen_height/2) - (window_height/2)
root.geometry(f"{window_width}x{window_height}+{x_coordinate}+{y_coordinate}")
# same result
# root.geometry("%dx%d+%d+%d" % (window_width, window_height, x_coordinate, y_coordinate))
root.mainloop()
以上返回错误几何说明符错误和值“400x200+568.0+332.0”。但是,当我尝试使用返回的内容并重新运行它时,这个问题得到了解决,但没有小数点。原来如此:
root = tk.Tk()
root.title("Testing")
root.geometry('400x200+568+332')
root.mainloop()
现在窗口居中。所以为此,我只是使用了楼层划分,它解决了这个问题。问题是我不确定是否是这种情况,因为我仍然需要将框架居中,因此了解正在发生的事情将有助于防止发生同样的问题。
【问题讨论】:
标签: python python-3.x tkinter