会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132654个问题
Python 全系列/第二阶段:Python 深入与提高/模块 1216楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 1218楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 1220楼

一、代码

# encoding=utf-8
"""测试Entry组件的基本用法,使用面向对象的方式"""
from tkinter import *
from tkinter import messagebox

class Application(Frame):      # 通常取名字为Application

    def __init__(self, master=None):
        super().__init__(master)        # super()代表的是父类的定义,而不是父类对象

        self.master=master
        self.pack()                     #调用布局管理器
        self.createWidget()

    def createWidget(self):
        self.codeHobby=IntVar()
        self.videoHobby=IntVar()

        print(self.codeHobby.get())    #默认值为0
        self.c1=Checkbutton(self,text="敲代码",
                            variable=self.codeHobby,onvalue=1,offvalue=0)
        self.c2 = Checkbutton(self, text="看视频",
                              variable=self.videoHobby,onvalue=1,offvalue=0 )

        self.c1.pack(side="left");self.c2.pack(side="left")

        Button(self,text="确定",command=self.confirm).pack(side="left")

    def confirm(self):
        if self.videoHoobby.get() ==1:
            messagebox.showinfo("测试","看视频是正常爱好")
        if self.codeHobby.get() ==1:
            messagebox.showinfo("测试","抓获野生动物一个")

if __name__=='__main__':        #只能作为脚本被运行;若作为模块,即import中是不会被运行的
    root = Tk()
    root.geometry("400x200+200+20")
    app = Application(master=root)
    root.mainloop()

二、运行结果

1.png

三、问题

老师,我这个运行怎么出错了。没得到预设的结果

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 1222楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 1225楼
Python 全系列/第二阶段:Python 深入与提高/模块 1227楼
Python 全系列/第二阶段:Python 深入与提高/坦克大战 1228楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 1229楼
Python 全系列/第二阶段:Python 深入与提高/坦克大战 1230楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637