鱼C论坛

 找回密码
 立即注册
查看: 2816|回复: 2

[已解决]tk不显示窗口

[复制链接]
发表于 2023-3-21 11:43:11 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
大佬帮忙看看这个tk为什么不显示GUI
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)
最佳答案
2023-3-21 11:53:14
fishccy 发表于 2023-3-21 11:48
我在IDLE中能跑出来,在pycharm中跑不出来,这时为什么

因为交互模式你不关闭shell 程序都没结束呀,所以窗口还在,

而你直接文本模式运行程序执行完就结束了,因为结束的太快所以你看不到窗口,想要他不结束的话加上 root.mainloop()

  1. from tkinter import *
  2. from tkinter import messagebox


  3. class Application(Frame):

  4.     def __init__(self, master=None):
  5.         super().__init__(master)
  6.         self.master = master
  7.         self.pack()
  8.         self.createWiget()

  9.     def createWiget(self):
  10.         self.codeHobby = IntVar()
  11.         self.videoHobby = IntVar()

  12.         print(self.codeHobby.get())
  13.         self.c1 = Checkbutton(self, text='敲代码', onvalue=1, offvalue=0)
  14.         self.c2 = Checkbutton(self, text='看电视', onvalue=1, offvalue=0)

  15.         self.c1.pack(side='left')
  16.         self.c2.pack(side='left')

  17.         Button(self, text='确定', command=self.confirm, ).pack()

  18.     def confirm(self):
  19.         if self.videoHobby.get() == 1:
  20.             messagebox.showinfo('看电视', '正经人都看电视')
  21.         if self.codeHobby.get() == 1:
  22.             messagebox.showinfo('敲代码', '正经人谁敲代码啊')


  23. if __name__ == '__main__':
  24.     root = Tk()
  25.     root.geometry('800x500+500+200')
  26.     root.title('checkbutton')
  27.     app = Application(master=root)
  28.     root.mainloop()                     # 加上这一行
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-3-21 11:48:30 | 显示全部楼层
我在IDLE中能跑出来,在pycharm中跑不出来,这时为什么
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-3-21 11:53:14 | 显示全部楼层    本楼为最佳答案   
fishccy 发表于 2023-3-21 11:48
我在IDLE中能跑出来,在pycharm中跑不出来,这时为什么

因为交互模式你不关闭shell 程序都没结束呀,所以窗口还在,

而你直接文本模式运行程序执行完就结束了,因为结束的太快所以你看不到窗口,想要他不结束的话加上 root.mainloop()

  1. from tkinter import *
  2. from tkinter import messagebox


  3. class Application(Frame):

  4.     def __init__(self, master=None):
  5.         super().__init__(master)
  6.         self.master = master
  7.         self.pack()
  8.         self.createWiget()

  9.     def createWiget(self):
  10.         self.codeHobby = IntVar()
  11.         self.videoHobby = IntVar()

  12.         print(self.codeHobby.get())
  13.         self.c1 = Checkbutton(self, text='敲代码', onvalue=1, offvalue=0)
  14.         self.c2 = Checkbutton(self, text='看电视', onvalue=1, offvalue=0)

  15.         self.c1.pack(side='left')
  16.         self.c2.pack(side='left')

  17.         Button(self, text='确定', command=self.confirm, ).pack()

  18.     def confirm(self):
  19.         if self.videoHobby.get() == 1:
  20.             messagebox.showinfo('看电视', '正经人都看电视')
  21.         if self.codeHobby.get() == 1:
  22.             messagebox.showinfo('敲代码', '正经人谁敲代码啊')


  23. if __name__ == '__main__':
  24.     root = Tk()
  25.     root.geometry('800x500+500+200')
  26.     root.title('checkbutton')
  27.     app = Application(master=root)
  28.     root.mainloop()                     # 加上这一行
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-24 21:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表