• 技术文章 >Python技术 >Python基础教程

    python Tkinter模块是什么

    小妮浅浅小妮浅浅2021-08-11 10:02:49原创5984

    说明

    1、Tkinter模块是Python的标准TkGUI工具包的接口。

    2、Tk和Tkinter可以在大多数Unix平台下使用,也可以应用于Windows和Macintosh系统。

    Tk8.0后续版本可以实现本地窗口风格,在绝大多数平台上运行良好。

    实例

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    70

    71

    72

    73

    74

    75

    76

    77

    78

    79

    80

    81

    82

    83

    84

    85

    86

    87

    88

    89

    90

    91

    92

    93

    94

    95

    96

    97

    98

    99

    100

    101

    102

    103

    104

    105

    106

    107

    108

    109

    110

    111

    112

    113

    114

    115

    116

    117

    118

    119

    120

    121

    122

    123

    124

    125

    126

    127

    import tkinter

    import time

    import threading

    from random import random

    from tkinter import messagebox as tkMessageBox

      

      

    class choujiang:

        # 初始化魔术方法

        def __init__(self):

            # 准备好界面

            self.root = tkinter.Tk()

            self.root.title('lowB版转盘')

            self.root.minsize(300, 300)

            # 声明一个是否按下开始的变量

            self.isloop = False

            self.newloop = False

            self.value = []

            # 调用设置界面的方法

            self.setwindow()

            self.root.mainloop()

      

      

        # 界面布局方法

        def setwindow(self):

            # 开始停止按钮

         self.btn_start = tkinter.Button(self.root, text='start/stop', command=self.newtask)

         self.btn_start.place(x=125, y=125, width=70, height=70)

      

      

         self.btn1 = tkinter.Button(self.root, text='1', bg='red')

         self.btn1.place(x=20, y=20, width=50, height=50)

      

      

         self.btn2 = tkinter.Button(self.root, text='2', bg='white')

         self.btn2.place(x=90, y=20, width=50, height=50)

      

      

         self.btn3 = tkinter.Button(self.root, text='3', bg='white')

         self.btn3.place(x=160, y=20, width=50, height=50)

      

      

         self.btn4 = tkinter.Button(self.root, text='3', bg='white')

         self.btn4.place(x=230, y=20, width=50, height=50)

      

      

         self.btn5 = tkinter.Button(self.root, text='3', bg='white')

         self.btn5.place(x=230, y=90, width=50, height=50)

      

      

         self.btn6 = tkinter.Button(self.root, text='2', bg='white')

         self.btn6.place(x=230, y=160, width=50, height=50)

      

      

         self.btn7 = tkinter.Button(self.root, text='1', bg='white')

         self.btn7.place(x=230, y=230, width=50, height=50)

      

      

         self.btn8 = tkinter.Button(self.root, text='3', bg='white')

         self.btn8.place(x=160, y=230, width=50, height=50)

      

      

         self.btn9 = tkinter.Button(self.root, text='2', bg='white')

         self.btn9.place(x=90, y=230, width=50, height=50)

      

      

         self.btn10 = tkinter.Button(self.root, text='3', bg='white')

         self.btn10.place(x=20, y=230, width=50, height=50)

      

      

         self.btn11 = tkinter.Button(self.root, text='1', bg='white')

         self.btn11.place(x=20, y=160, width=50, height=50)

      

      

         self.btn12 = tkinter.Button(self.root, text='3', bg='white')

         self.btn12.place(x=20, y=90, width=50, height=50)

      

      

          # 将所有选项组成列表

         self.girlfrends = [self.btn1, self.btn2, self.btn3, self.btn4, self.btn5, self.btn6, self.btn7, self.btn8,

                             self.btn9, self.btn10, self.btn11, self.btn12]

      

      

        def rounds(self):

            # 判断是否开始循环

            if self.isloop == True:

                return

            # 初始化计数 变量

            i = 0

            # 死循环

            while True:

                if self.newloop == True:

                    self.newloop = False

                    self.value = self.girlfrends[i - 1]['text']

                    if self.value =='1':

                        tkMessageBox.showinfo( "Winning Result", "恭喜获得一等奖 !'")

                    if self.value == '2':

                        tkMessageBox.showinfo("Winning Result", "恭喜获得二等奖 !")

                    if self.value == '3':

                        tkMessageBox.showinfo("Winning Result", '恭喜获得三等奖!')

                    return

                # 延时操作

                time.sleep(0.1)

                # 将所有的组件背景变为白色

                for x in self.girlfrends:

                    x['bg'] = 'white'

                # 将当前数值对应的组件变色

                self.girlfrends[i]['bg'] = 'red'

                # 变量+1

                i += 1

                # 如果i大于索引直接归零

                if i >= len(self.girlfrends):

                    i = 0

        # 建立一个新线程的函数

        def newtask(self):

            if self.isloop == False:

                # 建立线程

                t = threading.Thread(target=self.rounds)

                # 开启线程运行

                t.start()

                # 设置循环开始标志

                self.isloop = True

            elif self.isloop == True:

                self.isloop = False

                self.newloop = True

    #转盘效果

    c = choujiang()

    以上就是python Tkinter模块的介绍,希望对大家有所帮助。更多Python学习指路:python基础教程

    本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

    专题推荐:python tkinter模块
    上一篇:python归并排序和快速排序比较 下一篇:python美元转换成人民币转换代码

    相关文章推荐

    • python决策树算法是什么• python决策树算法的实现步骤• python如何判断文件夹内的重复图片• python一行输出10个数• Python输入三个数用空格隔开• python计算在月球的体重• python两种不同的文件流读写• python删除str中特定字符的方法• python如何将实例用作属性• python集合魔法函数有哪些• python实例创建销毁的函数整理• python三种属性管理魔法函数• python中高斯模糊是什么• python如何在二维图像上进行卷积• python读取txt文件• python中mock的断言使用• python中mock有哪些统计的方法• python异常是什么?如何解决?• python归并排序的基本思路• python快速排序的运作过程• python归并排序和快速排序比较

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网