正试图让我的按钮用我的窗口调整大小时头痛得厉害。 我已经尝试了各种各样的东西,但无济于事,我已经设法让按钮在调整大小时移动位置,但似乎无法将按钮绑定到屏幕上,我只是得到错误。
我已经将我的脚本缩小到只有一个功能,所以它更容易阅读,如果有人能指出我正确的方向,我会非常感激。
由于
#!/usr/bin/python
from Tkinter import*
resizeTest = Tk()
resizeTest.title('Resize Test')
resizeTest.geometry('220x100')
menubar = Menu(resizeTest)
#----------view menu
def normal():
resizeTest.geometry('220x100')
def enlarge():
resizeTest.geometry('500x360')
viewMenu = Menu(menubar, tearoff = 0)
viewMenu.add_command(label="Enlarge Alt+1", command=enlarge)
viewMenu.add_command(label="Normal Alt+2", command=normal)
menubar.add_cascade(label="View", menu=viewMenu)
buttons = Frame(resizeTest, bd=0, width=5, height=1, relief=GROOVE)
buttons.grid(column=0, row=1, padx=1)
numbers=["7", "4", "1", "8", "5", "2", "9", "6", "3"]
for index in range(9):
n=numbers[index]
Button(buttons, bg="White", text=n,width=5,height=1, relief=GROOVE).grid(padx=2,pady=2, row=index%3,column=index/3)
resizeTest.config(menu=menubar)
resizeTest.mainloop()
答案 0 :(得分:1)
你尝试过这样的事吗?可以在调整大小函数中调用添加用于创建具有宽度和高度参数的按钮的函数,以设置与窗口大小匹配的按钮。不完全自动绑定,但它可以工作。
#!/usr/bin/python
from Tkinter import*
resizeTest = Tk()
resizeTest.title('Resize Test')
resizeTest.geometry('220x100')
menubar = Menu(resizeTest)
#----------view menu
def normal():
buttons.grid_forget()
resizeTest.geometry('220x100')
add_buttons(5, 1)
def enlarge():
buttons.grid_forget()
resizeTest.geometry('500x360')
add_buttons(18, 6)
def add_buttons(w,h):
global buttons
buttons = Frame(resizeTest, bd=0, relief=GROOVE)
buttons.grid(column=0, row=1, padx=1)
numbers=["7", "4", "1", "8", "5", "2", "9", "6", "3"]
for index in range(9):
n=numbers[index]
Button(buttons, bg="White", text=n, width=w, height=h, relief=GROOVE).grid(padx=2,pady=2, row=index%3,column=index/3)
viewMenu = Menu(menubar, tearoff = 0)
viewMenu.add_command(label="Enlarge Alt+1", command=enlarge)
viewMenu.add_command(label="Normal Alt+2", command=normal)
menubar.add_cascade(label="View", menu=viewMenu)
add_buttons(5, 1)
resizeTest.config(menu=menubar)
resizeTest.mainloop()
答案 1 :(得分:1)
我已经知道了,不得不改用包方法但最后破解了它,谢谢你的帮助
#!/usr/bin/python
from Tkinter import *
class resizeTest:
def __init__( self, master ):
def normal():
self.ma.geometry('220x100')
def enlarge():
self.ma.geometry('500x360')
self.ma = master
self.ma.title('Resize Test')
self.ma.geometry('220x100')
self.f = Frame( self.ma )
self.f.pack(fill=BOTH, expand=YES)
self.enlarge = Button( self.f, text='Enlarge', height=1, width=10, padx=0, pady=1, command=enlarge)
self.enlarge.pack(side=LEFT, fill=BOTH,expand=YES)
self.normal = Button( self.f, text='Normal', height=1, width=10, padx=0, pady=1, command=normal)
self.normal.pack(side=RIGHT, fill=BOTH,expand=YES)
root = Tk()
app = resizeTest(root)
root.mainloop()