tkinter 图片浏览器
时间: 2023-10-11 13:10:23 浏览: 47
要使Tkinter窗口居中,可以使用以下代码:
```python
from tkinter import *
class MyFrm(Frame):
def __init__(self, master):
self.root=master
self.screen_width = self.root.winfo_screenwidth()
self.screen_height = self.root.winfo_screenheight()
self.root.update_idletasks()
self.root.withdraw()
self.root.geometry('%dx%d+%d+%d' % (self.root.winfo_width(), self.root.winfo_height(),
(self.screen_width - self.root.winfo_width()) / 2,
(self.screen_height - self.root.winfo_height()) / 2))
self.root.deiconify()
if __name__=='__main__':
root=Tk()
MyFrm(root)
root.mainloop()
```
在这个代码中,我们首先获得屏幕的宽度和高度,然后计算出窗口应该位于屏幕中心的位置,并使用`geometry`方法来设置窗口的位置。具体而言,`geometry`方法的参数是一个字符串,以`%dx%d+%d+%d`的格式指定窗口的宽度、高度、左上角的x坐标和y坐标。通过将窗口的宽度和高度设置为`self.root.winfo_width()`和`self.root.winfo_height()`,我们可以确保窗口的尺寸不会改变。而将窗口的左上角的x坐标和y坐标设置为`(self.screen_width - self.root.winfo_width()) / 2`和`(self.screen_height - self.root.winfo_height()) / 2`,我们可以将窗口放置在屏幕的中心位置。
请注意,上述代码中的`MyFrm`类是继承自`Frame`的自定义类,我们在类的初始化方法`__init__`中实现了窗口居中的功能。
另外,引用和引用中提到的`screen_height=root.winfo_screenheight()-100`和`screen_height = root.winfo_screenheight() - 100`并没有被使用到。因此,我们只需关注上述所提供的代码实现即可。