python sample GUI Tkinter my use

这篇博客介绍了如何利用Python的Tkinter库创建简单的图形用户界面(GUI)。通过实例代码,展示了如何设置窗口、添加控件以及响应用户交互,为初学者提供了清晰的指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# -*- coding: utf-8 -*-
#!/usr/bin/env python2
import sys
import os 
from Tkinter import *           # 导入 Tkinter 库
import time
import threading
root = Tk()                     # 初始化Tk()
root.title("GUI-Test-Read thermal")        # 设置窗口标题
root.geometry("500x300")        # 设置窗口大小 注意:是x 不是*
root.resizable(width=False, height=False) # 设置窗口是否可以变化长/宽,False不可变,True可变,默认为True
#root.iconbitmap('\heat.ico')
li     = ['1','2','3','4','5','6']
#movie  = ['7','8','9']
listb  = Listbox(root)      
#listb2 = Listbox(root)
for item in li:                 
    listb.insert(0,item)
 
#for item in movie:              
#    listb2.insert(0,item)  
def SaveLogfile(Str):
    f=open('.\\GUI_log.txt','a+')  
    print>>f,Str
    f.close()
def addlist():
    if listb.get(0)=="":
     a=int("0")
    else:
     a=int(listb.get(0))    
    a=a+1;
    listb.insert(0,str(a))
#    if a==15:
#     listb2.destroy()
    if a==22:
     #listb.clear
     listb.delete(0,END)
    if a==90:
     root.destroy()
def Button1cmd():
    #listb.delete(0,END)
    Tstr=time.strftime('%m/%d-%H:%M:%S',time.localtime(time.time()))
    listb.insert(0,Tstr)
    SaveLogfile(Tstr)
def Button2cmd():
     root.destroy() 
def Button3cmd():     
    Tstr=time.strftime('%m/%d-%H:%M:%S',time.localtime(time.time()))
    listb.insert(0,Tstr)    
var1 = IntVar()
Checkbutton(root, text="Auto_Raed", variable=var1).place(x=6, y=40) 

def fun_timer():
    button1.place(x=6, y=130)
    if var1.get()==1:
      Button3cmd(); 
    global timer
    timer = threading.Timer(2.0, fun_timer)
    timer.start()

timer = threading.Timer(1, fun_timer)
timer.start()

v1 = StringVar()
p1 = StringVar()    
def Button4cmd():
    if ''==v1.get():
      return;
    if ''==p1.get():
      return;
    print (v1.get())
    print (p1.get())    
    
###############################################################################   grid
def get_screen_size(window):
    return window.winfo_screenwidth(),window.winfo_screenheight()
def get_window_size(window):
    return window.winfo_reqwidth(),window.winfo_reqheight() 
def center_window(root, width, height):
    screenwidth = root.winfo_screenwidth()
    screenheight = root.winfo_screenheight()
    size = '%dx%d+%d+%d' % (width, height, (screenwidth - width)/2, (screenheight - height)/2)
    #print(size)
    root.geometry(size) 
center_window(root, 800,500)
###############################################################################
Button(root, text="press_Read", command=addlist).place(x=6, y=60) 
button1 = Button(root, text="press_Clear", command=Button1cmd) 
button1.place(x=6, y=130) 

button4 = Button(root, text="get-value", command=Button4cmd) 
button4.place(x=280, y=460) 

button3 = Button(root, text="Set-Timer", command=Button3cmd) 
button3.place(x=6, y=180) 
button2 = Button(root, text="press_Exit", command=Button2cmd) 
button2.place(x=6, y=280)

listb.place(x=100, y=6, height=380, width=490)
#listb2.grid(column=30, row=0)
def test(content):
    if content.isdigit() or content == "":
        return True
    else:
        return False
test_cmd = root.register(test)
Label1 = Label(root,text='Input Fan 1 speed:').place(x=8,y=430)
Label2 = Label(root,text='Input Fan 2 speed:').place(x=8,y=460)

e1 = Entry(root,textvariable=v1,
           validate='key',
           validatecommand=(test_cmd, '%P') ).place(x=120,y=430)
e2 = Entry(root,textvariable=p1,
           validate='key',
           validatecommand=(test_cmd, '%P') ).place(x=120,y=460)

root.mainloop()                

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值