python中tkinter的使用-上

00基础代码

import tkinter

win = tkinter.Tk()
win.title("Liuwang")
win.geometry("400x400+200+20")



win.mainloop()

01简单示例

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

win.mainloop()

02Label控件

import tkinter


win = tkinter.Tk()
win.title("Liuwang")
win.geometry("400x400+200+20")

'''
Label:标签控件
可以显示文本
'''
#win:父窗体
#bg:背景色
#fg :字体颜色
#text:文本内容
# wraplength:制定text文本中多款进行换行
#justify :设置换行后的对齐方式
#anchor :位置 n北   e东   s南  w西  center居中   ne东北.....
label = tkinter.Label(win,
                      text = "LIUwang",
                      bg = "blue",
                      fg = "red",
                      font = ("黑体",20),
                      width =10 ,
                      height=10,
                      wraplength =100,
                      justify ="left",
                      anchor = "center"
                      )
#显示出来
label.pack()



win.mainloop()

03Button控件

import tkinter

def func():
    print("Liuwang is handesome")


win = tkinter.Tk()
#设置标题
win.title("Liuwang")
#设置大小和位置
win.geometry("400x400+200+20")
#进入消息循环

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

button2 = tkinter.Button(win,text="按钮",command = win.quit)
button2.pack()



win.mainloop()

04entry控件


import tkinter


win = tkinter.Tk()
win.title("Liuwang")
win.geometry("400x400+200+20")

'''
输入控件
用于显示简单的文本内容

'''
#show="*"密文显示
#绑定变量
e = tkinter.Variable()


entry = tkinter.Entry(win,textvariable= e)
entry.pack()

#e就代表输入框这个对象
e.set("Liuwang is handsome")
print(e.get())
print(entry.get())


win.mainloop()




05点击按钮输入框内容


import tkinter


win = tkinter.Tk()
win.title("Liuwang")
win.geometry("400x400+200+20")

def showInfo():
    print(entry.get())

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




win.mainloop()

06Text控件

import tkinter

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


'''
文本控件,用于显示多行文本
'''

#height :显示行数

text = tkinter.Text(win,width = 30 ,height = 4 ,)
text.pack()
str ="简介:印度阿三,指印度人,带有种族歧视意味的贬义称呼,阿SIR音译。“印度阿三”来自“十里洋场”时期的吴语上海话,吴人极喜加“阿”字,而上海话中与“三”相关的词汇(阿三、八三、瘪三、十三点、猪头三)多为贬义词。上海当年的英租界中经常会有从印度调来的“公务员”,负责一些杂事,而这些印度人"
text.insert(tkinter.INSERT,str)




win.mainloop()

07带滚动条的Text

import tkinter

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


'''
文本控件,用于显示多行文本
'''



#创建滚动条
scroll = tkinter.Scrollbar()
#height :显示行数
text = tkinter.Text(win,width = 50 ,height = 8 ,)
#side放到窗体的哪一侧
scroll.pack(side = tkinter.RIGHT,fill = tkinter.Y)

text.pack(side = tkinter.LEFT,fill = tkinter.Y)
#关联
scroll.config(command = text.yview)
text.config(yscrollcommand = scroll.set)


str ="简介:印度阿三,指印度人,带有种族歧视意味的贬义称呼,阿SIR音译。“印度阿三”来自“十里洋场”时期的吴语上海话,吴人极喜加“阿”字,而上海话中与“三”相关的词汇(阿三、八三、瘪三、十三点、猪头三)多为贬义词。上海当年的英租界中经常会有从印度调来的“公务员”,负责一些杂事,而这些印度人"
text.insert(tkinter.INSERT,str)




win.mainloop()

08CheckButton多选框控件

import tkinter
win = tkinter.Tk()
win.title("Liuwang")
win.geometry("400x400+200+20")

def update():
    message=""
    if hobby1.get()==True:
        message += "money\n"
    if hobby2.get()==True:
        message += "power\n"
    if hobby3.get()==True:
        message += "people\n"
    #清除text中的所有内容
    text.delete(0.0,tkinter.END)
    text.insert(tkinter.INSERT,message)

# 要绑定的变量
hobby1 = tkinter.BooleanVar()
#多选框
check1 = tkinter.Checkbutton(win,text="money",
                             variable= hobby1,command = update)
check1.pack()
hobby2 = tkinter.BooleanVar()
check2 = tkinter.Checkbutton(win,text="power",
                             variable=hobby2, command=update)
check2.pack()
hobby3 = tkinter.BooleanVar()
check3 = tkinter.Checkbutton(win,text="people",
                             variable = hobby3, command = update)
check3.pack()

text = tkinter.Text(win,height= 5,width = 50)
text.pack()



win.mainloop()

09RadiioButton单选框控件

import tkinter

win = tkinter.Tk()
win.title("Liuwang")
win.geometry("400x400+200+20")

def update():
    print(r.get())

#一组单选框要绑定同一个变量
r= tkinter.IntVar()  #StringVar()
radio1 = tkinter.Radiobutton(win,text="one",value = 1,
                             variable= r,command= update)
radio1.pack()
radio2 = tkinter.Radiobutton(win,text="two",value = 2,
                             variable= r,command= update)
radio2.pack()






win.mainloop()

猜你喜欢

转载自blog.csdn.net/qq_41856814/article/details/89398169
今日推荐