Python tkinter2021-08-27

1、简单示例

import tkinter
#创建主窗口
win = tkinter.Tk()
#设置标题
win.title("fanfan")
#设置大小和位置
win.geometry("400x400+200+0")
#进入消息循环

win.mainloop()

2、label空间

import tkinter
win = tkinter.Tk()
win.title("sunck")
win.geometry("400x400+200+0")
'''
label:标签控件可以显示文本
'''
#参数1:win父窗体
#text:显示文本内容
#bg:背景色
#fg:字体颜色
#font 字体及大小
#width
#height
# wrapLength指定text文本中多宽进行换行
#justify 设置换行后的对齐方式
# anochor 位置n北   e东   s南    w西   center居中 ne  se   sw  nw
label = tkinter.Label(win,
                      text="fanfan",
                      bg="blue",
                      fg = "red",
                      font=("黑体", 20),
                      width=10,
                      height=4,
                      wraplength=100,
                      justify="left",
                      anchor="center")
#显示出来
label.pack()

win.mainloop()

3、button控件

import tkinter
win = tkinter.Tk()
win.title("fanfan")
win.geometry("400x400+200+0")
def func():
    print("fanfan is a good man")
#创建按钮
button1 = tkinter.Button(win, text="按钮", command=func, width=10, height=10)
button1.pack()

button2 = tkinter.Button(win, text="按钮", command=lambda:print("fanfan is a good man"), width=10, height=10)
button2.pack()

button3 = tkinter.Button(win, text="按钮", command=win.quit, width=10, height=10)
button3.pack()

win.mainloop()

4、entry控件

import tkinter
win = tkinter.Tk()
win.title("fanfan")
win.geometry("400x400+200+0")
'''
输入控件
用于显示简单的文本内容
'''
#绑定变量
e = tkinter.Variable()
# show密文显示   show="*"
entry = tkinter.Entry(win, textvariable=e)
entry.pack()

#e就代表输入框这个对象
#设置值
e.set("fanfan is a good man")
#取值
print(e.get())
print(entry.get())

win.mainloop()

5、点击按钮输出输入框中的内容

import tkinter
win = tkinter.Tk()
win.title("fanfan")
win.geometry("400x400+200+0")
def showInfo():
    print(entry.get())
entry = tkinter.Entry(win)
entry.pack()

button = tkinter.Button(win, text="按钮", command=showInfo)
button.pack()

win.mainloop()

6、text控件

import tkinter
win = tkinter.Tk()
win.title("fanfan")
win.geometry("400x400+200+0")
'''
文本控件,用于显示多行文本
'''
# height显示的行数
text = tkinter.Text(win, width=30, height=4)
text.pack()

str = """That we are in the midst of crisis is now well understood. Our nation is at war, against a far-reaching network of violence and hatred. Our economy is badly weakened, a consequence of greed and irresponsibility on the part of some, but also our collective failure to make hard choices and prepare the nation for a new age. Homes have been lost; jobs shed; businesses shuttered. Our health care is too costly; our schools fail too many; and each day brings further evidence that the ways we use energy strengthen our adversaries and threaten our planet."""
text.insert(tkinter.INSERT, str)

win.mainloop()

7、带滚动条的text

import tkinter
win = tkinter.Tk()
win.title("fanfan")
# win.geometry("400x400+200+0")
'''
文本控件,用于显示多行文本
'''
#创建滚动条
scroll = tkinter.Scrollbar()

text = tkinter.Text(win, width=50, height=8)
#side放到窗体的右侧
# fill填充
scroll.pack(side=tkinter.RIGHT, fill=tkinter.Y)
text.pack(side=tkinter.LEFT, fill=tkinter.Y)

str = """That we are in the midst of crisis is now well understood. Our nation is at war, against a far-reaching network of violence and hatred. Our economy is badly weakened, a consequence of greed and irresponsibility on the part of some, but also our collective failure to make hard choices and prepare the nation for a new age. Homes have been lost; jobs shed; businesses shuttered. Our health care is too costly; our schools fail too many; and each day brings further evidence that the ways we use energy strengthen our adversaries and threaten our planet.That we are in the midst of crisis is now well understood. Our nation is at war, against a far-reaching network of violence and hatred. Our economy is badly weakened, a consequence of greed and irresponsibility on the part of some, but also our collective failure to make hard choices and prepare the nation for a new age. Homes have been lost; jobs shed; businesses shuttered. Our health care is too costly; our schools fail too many; and each day brings further evidence that the ways we use energy strengthen our adversaries and threaten our planet."""
text.insert(tkinter.INSERT, str)
#关联
scroll.config(command=text.yview)
text.config(yscrollcommand=scroll.set)


win.mainloop()

8、checkbutton多选框控件

import tkinter
win = tkinter.Tk()
win.title("fanfan")
win.geometry("400x400+200+0")
def updata():
    message = ""
    if hobby1.get() == True:
        message += "money\n"
    if hobby2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值