python的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)




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章