Python实验项目7 :tkinter GUI编程

**1)**利用tkinter 制作界面,效果图如下:

python 复制代码
from tkinter import *  # 
win=Tk()
for i in range(1,20):
    Button(width=5,height=10,bg="black" if i%2==0 else
     "white").pack(side="left")
win.geometry("880x300")
win.mainloop()

(2)利用tkinter 制作界面,效果图如下:

python 复制代码
from tkinter import *
win=Tk()
win.title("bind()绑定")
win.geometry("300x200")
frame1=LabelFrame(relief=GROOVE,text="工具栏")
frame1.pack(anchor=NW,fill=X)
btn1=Button(frame1,text="复制")
btn1.grid(row=1,column=1)
btn2=Button(frame1,text="剪切")
btn2.grid(row=1,column=2)
btn3=Button(frame1,text="粘贴")
btn3.grid(row=1,column=3)
text1=Text()
text1.pack(expand=YES,fill=BOTH)

def docopy():
    data=text1.get(SEL_FIRST,SEL_LAST)
    text1.clipboard_clear()
    text1.clipboard_append(data)
def docut():
    data=text1.get(SEL_FIRST,SEL_LAST)
    text1.delete(SEL_FIRST,SEL_LAST)
    text1.clipboard_clear()
    text1.clipboard_append(data)
def dopaste():
    text1.insert(INSERT,text1.clipboard_get())
def doclear():
    text1.delete('1.0',END)
btn1.config(command=docopy)
btn2.config(command=docut)
btn3.config(command=dopaste)
win.geometry("600x300")
mainloop()

(3) 设计GUI界面,模拟QQ登录界面,用户输入用户名和密码,如果正确提示登录成功;否则提示登录失败。

python 复制代码
from tkinter import *  # 
import tkinter.messagebox
win=Tk()
lable=Label(text="QQ")
lable.grid(row=0,column=0,columnspan=2)
lable=Label(text="用户名")
lable.grid(row=1,column=0)
lable=Label(text="密码")
lable.grid(row=2,column=0)
win.geometry("400x300")
username=StringVar()
entry=Entry(textvariable=username)
entry.grid(row=1,column=1)
password=StringVar()
entry1=Entry(textvariable=password)
entry1.grid(row=2,column=1)
button=Button(text="reset")
button.grid(row=3,column=0)
button1=Button(text="OK")
button1.grid(row=3,column=1)
def reset():
    password.set("")
    username.set("")

def ok():   
    if(password.get()=="123456" and username.get()=="周杰伦"):
        tkinter.messagebox.showinfo(title="登录界面",message="登录成功")
    else:
        tkinter.messagebox.showinfo(title="登录界面",message="用户名或密码错误")
button.config(command=reset)  
button1.config(command=ok) 
win.mainloop()

(4) 使用Button组件的command参数实现事件处理,将事件处理的方法使用bind()方法实现。

(5) 编制求两个正整形数最小公倍数程序。要求:两个输入框txt、txt2,用来输入整形数据;一个按钮;一个不可编辑的输入组件txt3。当单击按钮时,在txt3中显示两个整形数的最小公倍数的值。

python 复制代码
from tkinter import *
from tkinter import StringVar
win=Tk()
win.title("求两个正整形数最小公倍数")

label=Label(win,text="正整数1:")
label.grid(row=0,column=0)
label1=Label(win,text="正整数2:")
label1.grid(row=1,column=0)
label2=Label(win,text="最小公倍数:")
label2.grid(row=2,column=0)

win.geometry("300x300")

txt=StringVar()
entry=Entry(win,width=20,textvariable=txt)
entry.grid(row=0,column=1)

txt2=StringVar()
entry1=Entry(win,width=20,textvariable=txt2)
entry1.grid(row=1,column=1)

txt3=StringVar()
entry2=Entry(win,width=20,textvariable=txt3)
entry2.grid(row=2,column=1)

def func():
    a=int(txt.get())
    b=int(txt2.get())
    c=a*b
    for i in range(1,c+1):
        if a*i%b==0:
            txt3.set(a*i)
            break
def func2():
    txt.set("")
    txt2.set("")
    txt3.set("")


button = Button(text="计算", command=func)
button.grid(row=3,column=0)
button1 = Button(text="reset", command=func2)
button1.grid(row=3,column=1)
mainloop()

相关推荐
紫雾凌寒1 小时前
计算机视觉 |解锁视频理解三剑客——TimeSformer
python·深度学习·神经网络·计算机视觉·transformer·timesformer
程序员杰哥2 小时前
测试用例详解
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
go54631584657 小时前
本地部署 GitHub 上的 Python 人脸识别项目
开发语言·python·github
FreakStudio8 小时前
手把手教你用 MicroPython 玩转幻尔串口舵机,代码+教程全公开
python·嵌入式·大学生·面向对象·技术栈·电子diy·电子计算机
tekin8 小时前
基于 Python 开发在线多人游戏服务器案例解析
服务器·python·游戏·在线多人游戏服务器
让学习成为一种生活方式10 小时前
libGL.so.1: cannot open shared object file: No such file or directory-linux022
linux·开发语言·python
java1234_小锋11 小时前
一周学会Flask3 Python Web开发-Jinja2模板继承和include标签使用
python·flask·flask3
图书馆钉子户11 小时前
from flask_session import Session 为什么是Session(app)这么用?
python·flask·mybatis
一只蜗牛儿11 小时前
Sherpa-ONNX:说话人识别与语音识别自动开启(VAD)+ Python API 完整指南
人工智能·python·语音识别
结衣结衣.11 小时前
【OpenCV】入门教学
图像处理·人工智能·python·opencv