鱼C论坛

 找回密码
 立即注册
查看: 2264|回复: 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()
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()                     # 加上这一行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-3-21 11:48:30 | 显示全部楼层
我在IDLE中能跑出来,在pycharm中跑不出来,这时为什么
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-3-21 11:53:14 | 显示全部楼层    本楼为最佳答案   
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()                     # 加上这一行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-24 05:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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