当前位置:   article > 正文

python的GUI编程初探,这部分内容真的非常有意思_gui三要素

gui三要素

大家好,我是天空之城,今天给大家带来,python的GUI编程初探,这部分内容真的非常有意思


#感谢博友离愁无语说,海天一树X

import tkinter as tk
from tkinter import * #导入tkinter库


# top=tkinter.Tk()#创建窗口

#使窗口停下来,不一闪而去
# top.mainloop()

#--------------------------------------
'''
root = tkinter.Tk() #创建窗口

computerLanguages = ['C', 'C++', 'Python', 'Java']
humanLanguages = ['Chinese', 'English', 'Spanish']
listbox1 = Listbox(root)  #创建第一个标签盒子
listbox2 = Listbox(root)#创建第二个标签盒子
for item in computerLanguages:#从第一个列表中取出所有元素放入标签盒子
    listbox1.insert(0, item)

for item in humanLanguages:#从第二个列表中取出所有元素放入标签盒子
    listbox2.insert(0, item)

listbox1.pack()#将小部件放入主窗口中
listbox2.pack()
root.mainloop()#进入消息循环
'''
'''mainloop就是进入到事件(消息)循环。一旦检测到事件,就刷新组件。
譬如你输入一个字符,就要立即在光标那个位置显示出来(前提是你选中了文本框,也就是鼠标在文本框这个图案的范围内单击过)。
又譬如你点击了浏览器的首页按钮,那么就要清除你浏览器里的全部部件,然后重新绘制主页的布局和内容。
'''
#------------------------------------



#窗口添加按钮,设置按钮函数,一旦点击按钮就打印'hello button'

def clickButton():
    print('hello button')

root = Tk()

root.title('magic_box')
root.geometry('500x250+100+100')

Button(root, text='MyButton', command=clickButton).pack()

lablevar = tk.StringVar()
lablevar.set('开始学习python')


lable = tk.Label(root,textvariable=lablevar,bg='red',font=('宋体', 25),width=15, height=3)
#textvariable=lablevar,          # 标签的文字

lable.pack()

#使窗口停下来,不一闪而去
root.mainloop()

#-----------------------------------------------

6.线程小游戏,来挑选你的梦中女神吧。。


# 1.有12个备选选项和2个功能按钮 确定备选选项和功能按钮的位置

# 2.点击开始会不断旋转。选中的时候背景颜色为红色,点击停止结束

import threading

import tkinter

import time

# 1.实现窗口

root = tkinter.Tk()

root.title('美女联盟')

root.minsize(300,300)

# 2 摆放按钮
btn1 = tkinter.Button(root, text='苍老师', bg='red')
btn1.place(x=20, y=20, width=50, height=50)

btn2 = tkinter.Button(root, text='赵雅芝', bg='white')
btn2.place(x=90, y=20, width=50, height=50)

btn3 = tkinter.Button(root, text='李若彤', bg='white')
btn3.place(x=160, y=20, width=50, height=50)

btn4 = tkinter.Button(root, text='范冰冰', bg='white')
btn4.place(x=230, y=20, width=50, height=50)

btn5 = tkinter.Button(root, text='赵薇', bg='white')
btn5.place(x=230, y=90, width=50, height=50)

btn6 = tkinter.Button(root, text='林青霞', bg='white')
btn6.place(x=230, y=160, width=50, height=50)

btn7 = tkinter.Button(root, text='朱茵', bg='white')
btn7.place(x=230, y=230, width=50, height=50)

btn8 = tkinter.Button(root, text='张曼玉', bg='white')
btn8.place(x=160, y=230, width=50, height=50)

btn9 = tkinter.Button(root, text='刘岩', bg='white')
btn9.place(x=90, y=230, width=50, height=50)

btn10 = tkinter.Button(root, text='刘诗诗', bg='white')
btn10.place(x=20, y=230, width=50, height=50)

btn11 = tkinter.Button(root, text='高圆圆', bg='white')
btn11.place(x=20, y=160, width=50, height=50)

btn12 = tkinter.Button(root, text='邓紫棋', bg='white')
btn12.place(x=20, y=90, width=50, height=50)

#2.1将所有的选项放到列表中 目的:为了操作这些选项
hero_list = [btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10,btn11,btn12]


# 定义一个标记
stop_sign = False # 默认是停止

stop_id = None

# 3 定义一个函数 1.循环备选选项 2.设置选项颜色
def round():

    global stop_id

    i = 1

    if isinstance(stop_id,int):

        i = stop_id

    while True:

        time.sleep(0.3)


        for x in hero_list:

            x['bg'] = 'white'


        hero_list[i]['bg'] = 'red'


        i += 1

        print('当前的i为:',i)

        if i >= len(hero_list):

            i = 0

        if stop_sign == True:

            stop_id = i

            break

# 4 定义停止的方法
def stop():

    global stop_sign

    if stop_sign == True:

        return

    stop_sign = True




# 5 定义开始的方法

def newtask():

    global stop_sign

    stop_sign = False


    t = threading.Thread(target=round)


    t.start()

# 6.设置按钮
btn_start = tkinter.Button(root,text='开始',command=newtask)

btn_start.place(x=90,y=125,width=50,height=50)




  • 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
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/775447
推荐阅读
相关标签
  

闽ICP备14008679号