【Python】窗口界面 按钮 文本框

python窗口


一、窗口界面生成

import tkinter

# 命名win为窗口类型对象
win = tkinter.Tk()
# 窗口属性的设置

win.title("示例")
win.geometry("500x600+200+50")

# 窗口显示
win.mainloop()

win.title(“”)引号内为窗口名称

win.geometry(“a*b+c+d”)
参数 a 是指窗口的宽度((横向长度),参数 b 是窗口的高度(纵向长度)
参数 c 和 d 是窗口左上点距离屏幕最左上点的距离
c 是横向距离,d 是纵向距离


二、按钮设置

def func():
    print("hello world")

button = tkinter.Button(win, text="按钮",  width = 30, height = 2, command=func)
button.place(x=10, y=320)

PS:按钮的width和height是以文本字符为大小的
tkinter.Button(A,text=“B”,width = C , height = D ,command = E)
如上所示的例子中:
A:窗口对象的名称
B:按钮的名称
C:宽度,横向长度
D:高度,纵向长度
E:本案例中是命名了一个函数 func() ,然后执行函数中的代码,也可以直接设置成简单的函数

botton.place(x= a , y= b)
a:距离窗口界面左上点的横向距离
b:距离窗口界面左上点的纵向距离


之后如果要想改变按钮的属性,可以参考如下代码:

button['width']=225
button['height']=365

三、文本框设置

import tkinter


def func():
    bs1 = bs_1.get() 
    bs2 = bs_2.get()
    a = int(bs1)
    b = int(bs2)
    print(a + b)


win = tkinter.Tk()
win.title("窗体")
win.geometry("400x400+200+50")

bs_1 = tkinter.Entry(win)
bs_1.pack()
bs_1.place(width=300, height=20, x=50)

bs_2 = tkinter.Entry(win)
bs_2.pack()
bs_2.place(width=300, height=20, x=50, y=50)

button = tkinter.Button(win, text="确定", width=30, height=2, command=func)
button.place(x=100, y=320)

win.mainloop()

结果如图所示
ceshi
bs_1和bs_2是文本框对象,bs_1.get()是获取bs_1文本框内的对象
bs_1.place()函数可以设置文本框的位置与大小

实例代码:

批量图片的重命名:

import cv2
import os
import numpy as np
import tkinter


def mas():
    s1=entry.get()
    s2=entry2.get()
    i = 1
    for filename in os.listdir(s1):
        img = cv2.imdecode(np.fromfile(s1 + '\\' + filename, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
        cv2.imwrite(s2 + '\\' + str(i) + '.jpg', img)  # 保存图片
        i = i + 1


win = tkinter.Tk()
win.title("窗体")
win.geometry("400x400+200+50")

entry = tkinter.Entry(win)
entry.pack()
entry.place(width=300, height=20, x=50)

entry2 = tkinter.Entry(win)
entry2.pack()
entry2.place(width=300, height=20, x=50, y=50)


button = tkinter.Button(win, text="确定", width=30, height=2, command=mas)
button.place(x=100, y=320)

win.mainloop()

在第一个文本框中输入起始地址,在第二个文本框中输入保存地址,点击确定可以执行任务,如下所示:
在这里插入图片描述

  • 17
    点赞
  • 59
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

子非_s

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值