2

我想根据屏幕尺寸使用比率将窗口框架广告设置为中心。但是我看不到在哪里正确修改我的代码来执行这样的程序。我的程序是以下示例:

class App:
    def __init__(self,master):
        ScreenSizeX = master.winfo_screenwidth()  # Get screen width [pixels]
        ScreenSizeY = master.winfo_screenheight() # Get screen height [pixels]
        ScreenRatio = 0.8                              # Set the screen ratio for width and height
        FrameSizeX  = int(ScreenSizeX * ScreenRatio)
        FrameSizeY  = int(ScreenSizeY * ScreenRatio)
        FramePosX   = (ScreenSizeX - FrameSizeX)/2 # Find left and up border of window
        FramePosY   = (ScreenSizeY - FrameSizeY)/2

        print FrameSizeX,FrameSizeY,FramePosX,FramePosY

        #geometry(str(self.winfo_screenwidth())+"x"+str(self.winfo_screenheight())+"+0+0")
        frame = Tkinter.Frame(master)
        frame.pack()

        self.button = Tkinter.Button(frame,text="Quit",fg="red",command=frame.quit)
        self.button.pack()

        self.hi_there = Tkinter.Button(frame,text="Hi!",command=self.say_hi)
        self.hi_there.pack()

    def say_hi(self):
        print "hello !"

if __name__ == "__main__":
    root = Tkinter.Tk()
    app = App(root)
    root.mainloop()
4

2 回答 2

4

你为什么注释掉这geometry条线?它非常接近你真正需要的东西。尝试这个:

master.geometry("%sx%s+%s+%s" % (FrameSizeX,FrameSizeY,FramePosX,FramePosY))
于 2012-06-19T01:25:09.893 回答
1

这是适用于此功能的最终代码:

import Tkinter #Python integrated tool kit for interfaces

class App:
    def __init__(self,master):
        # Define frame size and position in the screen :
        ScreenSizeX = master.winfo_screenwidth()  # Get screen width [pixels]
        ScreenSizeY = master.winfo_screenheight() # Get screen height [pixels]
        ScreenRatio = 0.8                              # Set the screen ratio for width and height
        FrameSizeX  = int(ScreenSizeX * ScreenRatio)
        FrameSizeY  = int(ScreenSizeY * ScreenRatio)
        FramePosX   = (ScreenSizeX - FrameSizeX)/2 # Find left and up border of window
        FramePosY   = (ScreenSizeY - FrameSizeY)/2
        master.geometry("%sx%s+%s+%s"%(FrameSizeX,FrameSizeY,FramePosX,FramePosY))
        frame = Tkinter.Frame(master)
        frame.pack()

        self.button = Tkinter.Button(frame,text="Quit",fg="red",command=frame.quit)
        self.button.pack()

        self.hi_there = Tkinter.Button(frame,text="Hi!",command=self.say_hi)
        self.hi_there.pack()

    def say_hi(self):
        print "hello !"

if __name__ == "__main__":
    root = Tkinter.Tk()
    app = App(root)
    root.mainloop()
于 2012-06-19T06:46:12.567 回答