Table of Contents
1. Tkinter
What is GUI
Commonly used GUI libraries
2. wxPython
3. PyQT
四、总结
Home Backend Development Python Tutorial Using Python and Tkinter to implement a garbage classification answering application

Using Python and Tkinter to implement a garbage classification answering application

Apr 20, 2023 pm 07:55 PM
python tkinter

    1. Tkinter

    What is GUI

    Graphical User Interface (GUI for short) Also known as graphical user interface) refers to a computer operation user interface displayed graphically. A graphical user interface is an interface display format for human-computer communication that allows users to use input devices such as a mouse to manipulate icons or menu options on the screen to select commands, call files, start programs, or perform other daily tasks. Graphical user interfaces have many advantages over character interfaces that use the keyboard to enter text or character commands to complete routine tasks.

    The graphical user interface consists of windows, drop-down menus, dialog boxes and their corresponding control mechanisms. They are standardized in various new applications, that is, the same operations are always completed in the same way. In the graphical user interface, what users see and operate are graphical objects, and computer graphics technology is applied.

    GUI programming is similar to "building blocks", placing components (Widgets) into the window. The following is the drawing software in windows, which is a typical GUI program:

    Using Python and Tkinter to implement a garbage classification answering application

    Commonly used GUI libraries

    1. Tkinter

    tkinter (Tk interface) is Python's standard GUI library, supporting cross-platform GUI program development. tkinter is suitable for writing small GUI programs, and is especially suitable for beginners to learn GUI programming. This time we will focus on tkinter.

    2. wxPython

    wxPython is a popular GUI library, suitable for large-scale application development, with stronger functions than tkinter, and the overall design framework is similar to MFC (Microsoft Foundation Classes).

    3. PyQT

    Qt is an open source GUI library suitable for the development of large-scale GUI programs. PyQT is the standard Python implementation of the Qt toolkit. We can also use the Qt Desginer interface designer to quickly develop GUI applications.

    A simplest Tkinter program should contain at least the following four parts:

    • ##Import the tkinter module

    • Create the main window, also called the root window (that is, the root window)

    • Add human-computer interaction controls and write the corresponding event functions

    • Display the main window through the main loop

    2. Final effect

    Let’s first take a look at the final effect of this project:

    Using Python and Tkinter to implement a garbage classification answering application

    After the project is run, the program will randomly select 10 questions from the question bank to test. When you answer correctly or incorrectly, there will be a pop-up window prompt. Each correct answer will get 10 points. When all the questions are answered, You will be prompted that the question has been completed and your final score for this exam will be displayed.

    3. Project Process

    3.1 Analysis Layout

    Since this is just a simple question answering program, the overall page does not need to be too complicated. First, a Label label is needed to display the title of each question, then there are 4 vertical (or horizontal) radio button buttons, and finally a click button for the next question is placed below.

    3.2 Create a window

    To do any project, you need to create the main window first, also called the root window (that is, the root window).

    # 导入本次项目用到的库
    import tkinter
    from tkinter import *
    from tkinter.messagebox import *
    import random
     
     
    if __name__ == '__main__':
        root = tkinter.Tk()  # 创建tkinter对象
        root.title('垃圾分类答题考试')  # 设置标题
        root.geometry("500x200+500+300")   # 设置页面的位置和长宽
        root.mainloop() # 让窗口一直显示出来
    Copy after login

    The effect is as follows:

    Using Python and Tkinter to implement a garbage classification answering application

    3.3 Build a question bank

    This time I am doing a question answering program for garbage classification, so I looked for it online Some questions about garbage classification. Here I directly use lists and tuples to store data. To explain, the first one in the tuple is the question, followed by the ABCD options, and finally the answer options for this question.

    # 准备一个题库列表
    object_list = [
    ('包了口香糖的纸巾属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('保鲜膜属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('变质的香肠属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('槟榔渣属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('剥掉的蛋壳属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('菜刀属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'C'), 
    ('茶叶渣应扔进哪个垃圾桶?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('吃剩的饼干渣是什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('抽完烟的烟蒂是什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('刀片属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'C'), 
    ('掉在地上的树叶是什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('废弃的食用油属于?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('甘蔗渣是什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'C'), 
    ('过期的化妆品是什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'B'), 
    ('过期的猫粮属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('包装药片的铝箔属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'B'), 
    ('喝茶剩下的茶叶渣是什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('花生壳属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('坏掉的电脑属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'C'), 
    ('灰色塑料袋属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('回形针属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('鸡骨头属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('鸡毛属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'C'), 
    ('家庭盆栽废弃的树叶是什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('碱性电池属于什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('胶卷属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'B'), 
    ('旧凉席属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('旧图书属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('空的灭火器属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'C'), 
    ('老旧电视机属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'C'), 
    ('落发属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('没用完的铅笔属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('没有泡过的茶叶属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('哪一类可进行资源再生利用?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'C'), 
    ('苹果手机电池属于什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'B'), 
    ('破碎的碗碟属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('染发剂的容器属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'B'), 
    ('杀虫剂的容器属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'B'), 
    ('水果硬糖属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('水银温度计属于什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'B'), 
    ('撕掉了的旧照片,应该丢到哪个垃圾桶内?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'B'), 
    ('塑料筷子属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'C'), 
    ('塑料玩具是什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'C'), 
    ('糖果包装纸属于什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('西瓜籽属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ]
    Copy after login

    3.4 Create components

    We analyzed the layout of this project earlier. We need 1 Label note, 4 radio buttons, and 1 next question button. Let’s start now They are placed in the main window, 10 questions are randomly selected from the question bank and the first question is displayed.

    # 导入本次项目用到的库
    import tkinter
    from tkinter import *
    from tkinter.messagebox import *
    import random
    # 准备一个题库列表
    object_list = [
    ('包了口香糖的纸巾属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('保鲜膜属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('变质的香肠属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('槟榔渣属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('剥掉的蛋壳属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('菜刀属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'C'), 
    ('茶叶渣应扔进哪个垃圾桶?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('吃剩的饼干渣是什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('抽完烟的烟蒂是什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('刀片属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'C'), 
    ('掉在地上的树叶是什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('废弃的食用油属于?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('甘蔗渣是什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'C'), 
    ('过期的化妆品是什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'B'), 
    ('过期的猫粮属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('包装药片的铝箔属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'B'), 
    ('喝茶剩下的茶叶渣是什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('花生壳属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('坏掉的电脑属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'C'), 
    ('灰色塑料袋属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('回形针属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('鸡骨头属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('鸡毛属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'C'), 
    ('家庭盆栽废弃的树叶是什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('碱性电池属于什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('胶卷属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'B'), 
    ('旧凉席属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('旧图书属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('空的灭火器属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'C'), 
    ('老旧电视机属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'C'), 
    ('落发属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('没用完的铅笔属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('没有泡过的茶叶属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('哪一类可进行资源再生利用?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'C'), 
    ('苹果手机电池属于什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'B'), 
    ('破碎的碗碟属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('染发剂的容器属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'B'), 
    ('杀虫剂的容器属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'B'), 
    ('水果硬糖属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('水银温度计属于什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'B'), 
    ('撕掉了的旧照片,应该丢到哪个垃圾桶内?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'B'), 
    ('塑料筷子属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'C'), 
    ('塑料玩具是什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'C'), 
    ('糖果包装纸属于什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('西瓜籽属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ]
     
    if __name__ == '__main__':
        root = tkinter.Tk()  # 创建tkinter对象
        root.title('垃圾分类答题考试')  # 设置标题
        root.geometry("500x200+500+300")   # 设置页面的位置和长宽
        # 从题库中随机抽取10个题目作为考试题
        values = random.sample(object_list,10)
        # 创建一个字符串变量用来记录选项值
        s = tkinter.StringVar()  
        s.set('E')  # 设置初始值为'E',初始没选中
        # 设置初始题号和分值
        num = 0
        score = 0
        label = tkinter.Label(root, text=values[num][0])  # 用来显示题目
        label.pack()
        # 创建第 1 个 Frame 组件
        f1 = Frame(root)  
        f1.pack()
        r1 = tkinter.Radiobutton(f1, variable=s, value='A', text=values[num][1])  # 安装第一个单选按钮
        r1.pack()
        r2 = tkinter.Radiobutton(f1, variable=s, value='B', text=values[num][2])  # 安装第二个单选按钮
        r2.pack()
        r3 = tkinter.Radiobutton(f1, variable=s, value='C', text=values[num][3])  # 安装第三个单选按钮
        r3.pack()
        r4 = tkinter.Radiobutton(f1, variable=s, value='D', text=values[num][4])  # 安装第四个单选按钮
        r4.pack()
            # 创建第 2 个 Frame 组件
        f2 = Frame(root)  
        f2.pack()
        # 创建下一题的按钮
        Button(f2, text='下一题').pack(side=LEFT)
        # 默认显示第一道题目
        
        label["text"] = str(num+1) + '.' + values[num][0]  # 显示题目
        # 显示4个选项
        r1["text"] = values[num][1]  
        r2["text"] = values[num][2]
        r3["text"] = values[num][3]
        r4["text"] = values[num][4]
        root.mainloop() # 让窗口一直显示出来
    Copy after login

    The effect is as follows:

    Using Python and Tkinter to implement a garbage classification answering application

    Now there is no response when you click on the next question, because at this time, only the layout of the page has been implemented, and it has not been implemented yet. The function of judging right from wrong.

    3.5 Write event function

    The logic of the event is that when you click the next question button, the program determines whether your options for this question are consistent with the answers in the question bank. If they are equal, it passes A pop-up window will be used to remind you that the answer is correct; if they are not equal, a pop-up window will be used to prompt you that the answer is wrong. After the prompt is completed, the next question will be displayed immediately. After each judgment, it is necessary to check whether the question is the last question. If so, stop answering the question and obtain the total score of this test.

    # 定义一个判断选项正确性的函数
    def main(values):
        # 全局引用num和score变量
        global num 
        global score
     
        # 如果选项和答案相等则答对了
        if s.get() == values[num][5]:
            showinfo("恭喜", "恭喜你答对了!")  # 提示你答对了
            score += 10  # 得分加10分
        # 如果选项和答案不相等则答错了
        else:
            showinfo("遗憾", "遗憾你答错了!")  # 提示你答错了
        num = num + 1  # 记录题号
        # 如果题号已经大于等于题目的总长度则需要进行结束并统计总分数
        if num >= len(values):
            showinfo("结果", f"全部题目做完了!\n您的最终得分为{score}分!")  # 提示题目做完了,总结出你的得分
            root.quit()  # 程序退出
            return
        
        # 显示下一题
        label["text"] = str(num+1) + '.' + values[num][0] # 显示题目
        # 显示4个选项
        r1["text"] = values[num][1]
        r2["text"] = values[num][2]
        r3["text"] = values[num][3]
        r4["text"] = values[num][4]
        s.set('E')  # 设置初始值为'E',初始没选中
    Copy after login

    At the same time, you also need to add the event function to the next question button

    Button(f2, text='下一题', command=lambda:main(values)).pack(side=LEFT)
    Copy after login

    A simple version of the answer program has been implemented here, and the effect is as shown before.

    四、总结

    本次使用了Python中的tkinter库实现了一个简易的垃圾分类答题程序,其中的题库和各种参数大家可以发挥自己的创意,自行进行修改,基于此源码的基础上创作出你的作品!

    源代码

    # 导入本次项目用到的库
    import tkinter
    from tkinter import *
    from tkinter.messagebox import *
    import random
     
    # 准备一个题库列表
    object_list = [
    ('包了口香糖的纸巾属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('保鲜膜属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('变质的香肠属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('槟榔渣属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('剥掉的蛋壳属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('菜刀属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'C'), 
    ('茶叶渣应扔进哪个垃圾桶?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('吃剩的饼干渣是什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('抽完烟的烟蒂是什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('刀片属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'C'), 
    ('掉在地上的树叶是什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('废弃的食用油属于?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('甘蔗渣是什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'C'), 
    ('过期的化妆品是什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'B'), 
    ('过期的猫粮属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('包装药片的铝箔属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'B'), 
    ('喝茶剩下的茶叶渣是什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('花生壳属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('坏掉的电脑属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'C'), 
    ('灰色塑料袋属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('回形针属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('鸡骨头属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('鸡毛属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'C'), 
    ('家庭盆栽废弃的树叶是什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('碱性电池属于什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('胶卷属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'B'), 
    ('旧凉席属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('旧图书属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('空的灭火器属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'C'), 
    ('老旧电视机属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'C'), 
    ('落发属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('没用完的铅笔属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('没有泡过的茶叶属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('哪一类可进行资源再生利用?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'C'), 
    ('苹果手机电池属于什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'B'), 
    ('破碎的碗碟属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('染发剂的容器属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'B'), 
    ('杀虫剂的容器属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'B'), 
    ('水果硬糖属于哪一类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ('水银温度计属于什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'B'), 
    ('撕掉了的旧照片,应该丢到哪个垃圾桶内?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'B'), 
    ('塑料筷子属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'C'), 
    ('塑料玩具是什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'C'), 
    ('糖果包装纸属于什么垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'A'), 
    ('西瓜籽属于哪类垃圾?', '其他垃圾', '有害垃圾', '可回收物', '厨余垃圾', 'D'), 
    ]
     
    # 定义一个判断选项正确性的函数
    def main(values):
        # 全局引用num和score变量
        global num 
        global score
     
        # 如果选项和答案相等则答对了
        if s.get() == values[num][5]:
            showinfo("恭喜", "恭喜你答对了!")  # 提示你答对了
            score += 10  # 得分加10分
        # 如果选项和答案不相等则答错了
        else:
            showinfo("遗憾", "遗憾你答错了!")  # 提示你答错了
        num = num + 1  # 记录题号
        # 如果题号已经大于等于题目的总长度则需要进行结束并统计总分数
        if num >= len(values):
            showinfo("结果", f"全部题目做完了!\n您的最终得分为{score}分!")  # 提示题目做完了,总结出你的得分
            root.quit()  # 程序退出
            return
        
        # 显示下一题
        label["text"] = str(num+1) + '.' + values[num][0] # 显示题目
        # 显示4个选项
        r1["text"] = values[num][1]
        r2["text"] = values[num][2]
        r3["text"] = values[num][3]
        r4["text"] = values[num][4]
        s.set('E')  # 设置初始值为'E',初始没选中
     
    if __name__ == '__main__':
        root = tkinter.Tk()  # 创建tkinter对象
        root.title('垃圾分类答题考试')  # 设置标题
        root.geometry("500x200+500+300")   # 设置页面的位置和长宽
        # 从题库中随机抽取10个题目作为考试题
        values = random.sample(object_list,10)
        # 创建一个字符串变量用来记录选项值
        s = tkinter.StringVar()  
        s.set('E')  # 设置初始值为'E',初始没选中
        # 设置初始题号和分值
        num = 0
        score = 0
        label = tkinter.Label(root, text=values[num][0])  # 用来显示题目
        label.pack()
        # 创建第 1 个 Frame 组件
        f1 = Frame(root)  
        f1.pack()
        r1 = tkinter.Radiobutton(f1, variable=s, value='A', text=values[num][1])  # 安装第一个单选按钮
        r1.pack()
        r2 = tkinter.Radiobutton(f1, variable=s, value='B', text=values[num][2])  # 安装第二个单选按钮
        r2.pack()
        r3 = tkinter.Radiobutton(f1, variable=s, value='C', text=values[num][3])  # 安装第三个单选按钮
        r3.pack()
        r4 = tkinter.Radiobutton(f1, variable=s, value='D', text=values[num][4])  # 安装第四个单选按钮
        r4.pack()
        # 创建第 2 个 Frame 组件
        f2 = Frame(root)  
        f2.pack()
        # 创建下一题的按钮
        Button(f2, text='下一题', command=lambda:main(values)).pack(side=LEFT)
        # 默认显示第一道题目
        
        label["text"] = str(num+1) + '.' + values[num][0]  # 显示题目
        # 显示4个选项
        r1["text"] = values[num][1]  
        r2["text"] = values[num][2]
        r3["text"] = values[num][3]
        r4["text"] = values[num][4]
        root.mainloop()
    Copy after login

    The above is the detailed content of Using Python and Tkinter to implement a garbage classification answering application. For more information, please follow other related articles on the PHP Chinese website!

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

    PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

    Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

    PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

    PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

    PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

    Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

    Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

    How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

    To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

    Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

    VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

    Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

    Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

    Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

    VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

    See all articles