tk不显示窗口
大佬帮忙看看这个tk为什么不显示GUIfrom tkinter import *
from tkinter import messagebox
class Application(Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.createWiget()
def createWiget(self):
self.codeHobby = IntVar()
self.videoHobby = IntVar()
print(self.codeHobby.get())
self.c1 = Checkbutton(self, text='敲代码', onvalue=1, offvalue=0)
self.c2 = Checkbutton(self, text='看电视', onvalue=1, offvalue=0)
self.c1.pack(side='left')
self.c2.pack(side='left')
Button(self, text='确定', command=self.confirm, ).pack()
def confirm(self):
if self.videoHobby.get() == 1:
messagebox.showinfo('看电视', '正经人都看电视')
if self.codeHobby.get() == 1:
messagebox.showinfo('敲代码', '正经人谁敲代码啊')
if __name__ == '__main__':
root = Tk()
root.geometry('800x500+500+200')
root.title('checkbutton')
app = Application(master=root) 我在IDLE中能跑出来,在pycharm中跑不出来,这时为什么{:10_266:} fishccy 发表于 2023-3-21 11:48
我在IDLE中能跑出来,在pycharm中跑不出来,这时为什么
因为交互模式你不关闭shell 程序都没结束呀,所以窗口还在,
而你直接文本模式运行程序执行完就结束了,因为结束的太快所以你看不到窗口,想要他不结束的话加上 root.mainloop()
from tkinter import *
from tkinter import messagebox
class Application(Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.createWiget()
def createWiget(self):
self.codeHobby = IntVar()
self.videoHobby = IntVar()
print(self.codeHobby.get())
self.c1 = Checkbutton(self, text='敲代码', onvalue=1, offvalue=0)
self.c2 = Checkbutton(self, text='看电视', onvalue=1, offvalue=0)
self.c1.pack(side='left')
self.c2.pack(side='left')
Button(self, text='确定', command=self.confirm, ).pack()
def confirm(self):
if self.videoHobby.get() == 1:
messagebox.showinfo('看电视', '正经人都看电视')
if self.codeHobby.get() == 1:
messagebox.showinfo('敲代码', '正经人谁敲代码啊')
if __name__ == '__main__':
root = Tk()
root.geometry('800x500+500+200')
root.title('checkbutton')
app = Application(master=root)
root.mainloop() # 加上这一行
页:
[1]