Setting a Frame Size and Location in Tkinter

半腔热情 提交于 2020-01-24 19:43:25

问题


I am trying to set a frame-size and the location of the frame (master widget) in Tkinter. Based off of this answer, I added this to my code:

from Tkinter import *
import ctypes
user = ctypes.windll.user32
screensize = (user.GetSystemMetrics(0), user.GetSystemMetrics(1), user.GetSystemMetrics(2), user.GetSystemMetrics(3))

class GetWord:
    def __init__(self, master):
        master.geometry("%s+%s+%s+%s" % (screensize[0], screensize[1], screensize[2], screensize[3]))
        # I added the above in, but not sure how it works
        self.frame = Frame(master, width = screensize[0], height = screensize[1])
        self.frame.grid()

However, when doing this I get a TclError:

Traceback (most recent call last):
  File #file path, line 39, in <module>
    f = GetWord(root)
  File #file path, line 8, in __init__
    master.geometry("%s+%s+%s+%s" % (screensize[0], screensize[1], screensize[2], screensize[3]))
  File "C:\Python2.7.3\lib\lib-tk\Tkinter.py", line 1534, in wm_geometry
    return self.tk.call('wm', 'geometry', self._w, newGeometry)
TclError: bad geometry specifier "1366+768+17+17"

I call the class like this:

root = Tk(className='derp')
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
f = GetWord(root)
root.mainloop()

How can I fix this? I want to have the frame start off in the center of the screen and start at a specific window size (right now it's full-screen but I will change that later on). Thanks!


回答1:


You Need to use the letter "x" instead of "+"

master.geometry("%sx%sx%sx%s" % (screensize[0], screensize[1], screensize[2], screensize[3]))


来源:https://stackoverflow.com/questions/14922937/setting-a-frame-size-and-location-in-tkinter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!