python窗口居中,在Tkinter类python中居中并调整窗口大小

I want to center a window frame ad set a size using a ratio according to screensize. But I cannot see where to modify my code correctly to perform such program. My program is the following example :

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()

解决方案

Why did you comment out the geometry line? It's quite close to what you need really. Try this:

master.geometry("%sx%s+%s+%s" % (FrameSizeX,FrameSizeY,FramePosX,FramePosY))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值