python tkinter按钮button的command事件触发问题

Ans_min 2020-03-19 01:52:47
自己最近在捯饬一些tkinter的东西,有个东西很困惑。为什么单独的一个窗口可以实现command
,加了一个窗口之后就不灵了。。。不知道是哪里影响到了,给出代码
on_hit = False
class QD:
def window_info(self):
info = tk.Tk()
info.title('补签界面')
info.geometry('250x150')
# label-补签的学生学号
label_info = tk.Label(info, text="输入补签的学生学号: ", font=('宋体', 10), height='1')
label_info.place(x=35, y=20, anchor='nw')
# entry-请输入学生学号
entry_info = tk.Entry(info)
entry_info.place(x=35, y=50, width="180", anchor='nw')
var = tk.StringVar()
label_tip = tk.Label(info, textvariable=var, font=('等线', 10))
label_tip.place(x=50, y=115, anchor='nw')
def click_sure():
global on_hit
if not on_hit:
on_hit = True
var.set('补签成功!请关闭窗口!')
else:
on_hit = False
var.set('')
button_sure = tk.Button(info, text='确定', width=15, height=1, command=click_sure)
button_sure.place(x=70, y=80, anchor='nw')

info.mainloop()
window = QD()
window.window_info()
测试是可以显示出label-tip

然而加上一个窗口就触发不了了:
on_hit = False
class QD:

def window_begin(self):
begin = tk.Tk()
begin.title('课堂点名器 3.0--开始点名界面')
begin.geometry('500x300')

# text-输出各种名单
text_list = tk.Text(begin, height="18")
text_list.pack()

# button-补签
def click_arrive():
self.window_info()
button_arrive = tk.Button(begin, text='补签', width=15, height=1, command=click_arrive)
button_arrive.place(x=180, y=250, anchor='nw')

# 返回
def callback():
if messagebox.askokcancel("退出", "是否退出点名环节?"):
begin.destroy()
begin.protocol("WM_DELETE_WINDOW", callback)

begin.mainloop()

def window_info(self):
info = tk.Tk()
info.title('补签界面')
info.geometry('250x150')

# label-补签的学生学号
label_info = tk.Label(info, text="输入补签的学生学号: ", font=('宋体', 10), height='1')
label_info.place(x=35, y=20, anchor='nw')
# entry-请输入学生学号
entry_info = tk.Entry(info)
entry_info.place(x=35, y=50, width="180", anchor='nw')

var = tk.StringVar()
label_tip = tk.Label(info, textvariable=var, font=('等线', 10))
label_tip.place(x=50, y=115, anchor='nw')

def click_sure():
global on_hit
if not on_hit:
on_hit = True
var.set('补签成功!请关闭窗口!')
else:
on_hit = False
var.set('')

button_sure = tk.Button(info, text='确定', width=15, height=1, command=click_sure)
button_sure.place(x=70, y=80, anchor='nw')

info.mainloop()


window = QD()
window.window_begin()
然后。。。。点击按钮就显示不出来了。。。。= =,到底是因为什么问题。。。
...全文
4034 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ans_min 2020-03-20
  • 打赏
  • 举报
回复
引用 4 楼 陈年椰子 的回复:
我用你的部分代码 写了 查题器

https://blog.csdn.net/seakingx/article/details/104969528


好的,被调用自己也挺高兴的
陈年椰子 2020-03-19
  • 打赏
  • 举报
回复
我用你的部分代码 写了 查题器 https://blog.csdn.net/seakingx/article/details/104969528
陈年椰子 2020-03-19
  • 打赏
  • 举报
回复
对, 子窗口都要这么用。 一个TK程序只能有一个 主 Tk()
Ans_min 2020-03-19
  • 打赏
  • 举报
回复
引用 1 楼 陈年椰子 的回复:
子窗口 要用 tk.Toplevel()



def window_info(self):
info = tk.Toplevel() # info = tk.Tk()
info.title('补签界面')
info.geometry('250x150')






这样试试


可以了,谢谢谢谢,可以问一下多个子窗口的话都可以这么用嘛?
陈年椰子 2020-03-19
  • 打赏
  • 举报
回复
子窗口 要用 tk.Toplevel()


 def window_info(self):
        info = tk.Toplevel()    #  info = tk.Tk()
        info.title('补签界面')
        info.geometry('250x150')


这样试试

37,742

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧