|
50鱼币
各位大佬,小弟在初学tkinter时碰到个问题,button组件关联的函数有多个动作的时候,或者说用户点了button之后的页面又有button,就变成函数里面套函数了,总感觉这样的写法不太对,请问有没有更简洁明了的方法呢?下面是我的button又button,函数套函数的写法:
另外顺便问下messagbox的弹出窗口如何固定位置在父窗口上,而不是屏幕中央的位置呢?
- from tkinter import *
- from tkinter.messagebox import *
- root = Tk()
- text = Text(root)
- text.pack()
- text.insert(INSERT, '这是主窗口,用户在此窗口上点确定后弹窗开始选择数字,选定点确定关闭弹窗,并获得值')
- def firstfunc():
- top = Toplevel(root)
- s = Scale(top, from_=11, to=42)
- s.pack()
-
- def secondfunc_in_firstfunc(s):
- if askyesno('提示', '这个窗口用于获取用户选定的值'):
- s = s.get()
- top.destroy()
- text.delete('1.0',END)
- text.insert(INSERT, '这是第二个函数,取得刚才用户选择的值:' + str(s))
- a = 10
- if s > a: #如果用户选择的值大于某个参数,再弹窗,问用户是否执行某个操作
- if askyesno('又弹窗','点确定执行下一步操作'):
- top2 = Toplevel(root)
- def thirdfun_in_secondfunc():
- top2.destroy()
- text.delete('1.0', END)
- text.insert(INSERT, '这是第三个函数,因为您选择的值大于%d而启动' %a)
- Button(top2,text='执行又一个函数', command=thirdfun_in_secondfunc).pack()
- Button(top, text='确定后,函数关闭此窗口,并获得选定的值', command=lambda: secondfunc_in_firstfunc(s)).pack()
- Button(root, text='确定', command=firstfunc).pack()
- mainloop()
复制代码 |
|