Python Tkinter 图形组件介绍

1、 窗口  Tkinter.Tk() 

# -*- coding: UTF-8 -*-
import Tkinter

myWindow = Tkinter.Tk()
myWindow.title('南风丶轻语')
myWindow.geometry('400x500')
myWindow.mainloop()

  运行结果

 

2、 标签 Label   Tkinter.Label()

# -*- coding: UTF-8 -*-
import Tkinter

myWindow = Tkinter.Tk()
myWindow.title('南风丶轻语')
myWindow.geometry('400x500')

textVar = Tkinter.StringVar()
textVar.set('我是一个标签')
mylabel = Tkinter.Label(myWindow, bg='yellow', width=20, height=1,textvariable=textVar)
mylabel.pack()

myWindow.mainloop()

  运行结果

 

3、 按钮 Tkinter.Button()

# -*- coding: UTF-8 -*-
import Tkinter

myWindow = Tkinter.Tk()
myWindow.title('南风丶轻语')
myWindow.geometry('400x500')

textVar = Tkinter.StringVar()
textVar.set('我是一个标签')
mylabel = Tkinter.Label(myWindow, bg='yellow', width=20, height=1,textvariable=textVar)
mylabel.pack()

print type(textVar.get())

clickFlag = False
def clickEvet():
    global clickFlag
    if clickFlag == False:
        textVar.set('我是被点击后的标签')
        clickFlag = True
    else:
        textVar.set('我是一个标签')
        clickFlag = False
        
myButton = Tkinter.Button(myWindow, width=10, height=1,text='点击', command=clickEvet)
myButton.pack()

myWindow.mainloop()

  运行结果

 

4、 输入框 Tkinter.Entry()

  备注 : 程序功能为点击按钮,标签显示 entry 中输入的内容

# -*- coding: UTF-8 -*-
import Tkinter

myWindow = Tkinter.Tk()
myWindow.title('南风丶轻语')
myWindow.geometry('400x500')

textVar = Tkinter.StringVar()
textVar.set('我是一个标签')
mylabel = Tkinter.Label(myWindow, bg='yellow', width=20, height=1,textvariable=textVar)
mylabel.pack()

def clickEvet():
    textVar.set(myEntry.get())
     
myButton = Tkinter.Button(myWindow, width=10, height=1,text='点击', command=clickEvet)
myButton.pack()

myEntry = Tkinter.Entry(myWindow, width=20,)
myEntry.pack()

myWindow.mainloop()

  运行结果

 

5、 文本框

  备注 : 实现功能为点击按钮,文本框插入数据(插入位置可以是鼠标位置,也可以是末尾,我们用两个按钮控制实现该效果)

# -*- coding: UTF-8 -*-
import Tkinter

myWindow = Tkinter.Tk()
myWindow.title('南风丶轻语')
myWindow.geometry('400x500')

myText = Tkinter.Text(myWindow, height=5, show=None)
myText.pack()

def clickEvet1():
    myText.insert('insert', '鼠标位置插入数据')
  
def clickEvet2():
    myText.insert('end', '末尾插入数据')
        
myButton = Tkinter.Button(myWindow, width=10, height=1,text='鼠标插入', command=clickEvet1)
myButton.pack()

myButton2 = Tkinter.Button(myWindow, width=10, height=1,text='末尾插入', command=clickEvet2)
myButton2.pack()

myWindow.mainloop()

  运行结果

 

6、 listBox

  备注 : 点击按钮,实现文本框显示选中的 listBox 的内容

# -*- coding: UTF-8 -*-
import Tkinter

myWindow = Tkinter.Tk()
myWindow.title('南风丶轻语')
myWindow.geometry('400x500')

myText = Tkinter.Text(myWindow, height=5, show=None)
myText.pack()
  
def clickEvet():
    myText.insert('end', myListBox.get(myListBox.curselection()))
        
myButton2 = Tkinter.Button(myWindow, width = 10, height = 1,text = '点击', command = clickEvet)
myButton2.pack()

listBoxVar = Tkinter.StringVar()
listBoxVar.set((11, 22, 33, 'tan'))
myListBox = Tkinter.Listbox(myWindow, listvariable = listBoxVar)
myListBox.pack()

myWindow.mainloop()

  运行结果

 

7、CheckBox

  备注:实现选中 CheckBox, 标签中显示选中内容

# -*- coding: UTF-8 -*-
import Tkinter

myWindow = Tkinter.Tk()
myWindow.title('CheckButton Test Window')
myWindow.geometry('430x430')

labelText = Tkinter.StringVar()
lable = Tkinter.Label(myWindow, textvariable=labelText, bg='yellow', width=30, height=1)
lable.pack(side='top')


def chooseEvent():
    if (chooseFirst.get() == 1) and (chooseSecond.get() == 0) and (chooseThird.get() == 0):
        labelText.set('Tan')
    elif (chooseFirst.get() == 0) and (chooseSecond.get() == 1) and (chooseThird.get() == 0):
        labelText.set('Xiao')
    elif (chooseFirst.get() == 0) and (chooseSecond.get() == 0) and (chooseThird.get() == 1):
        labelText.set('Hui')
    elif (chooseFirst.get() == 1) and (chooseSecond.get() == 1) and (chooseThird.get() == 0):
        labelText.set('TanXiao')
    elif (chooseFirst.get() == 1) and (chooseSecond.get() == 0) and (chooseThird.get() == 1):
        labelText.set('TanHui')
    elif (chooseFirst.get() == 0) and (chooseSecond.get() == 1) and (chooseThird.get() == 1):
        labelText.set('XiaoHui')
    elif (chooseFirst.get() == 1) and (chooseSecond.get() == 1) and (chooseThird.get() == 1):
        labelText.set('TanXiaoHui')
    else:
        labelText.set('')
    
chooseFirst = Tkinter.IntVar()
checkButton1 = Tkinter.Checkbutton(myWindow, text='Tan', variable=chooseFirst, onvalue=1, offvalue=0, command=chooseEvent)
checkButton1.place(x=200, y=22)
chooseSecond = Tkinter.IntVar()
checkButton2 = Tkinter.Checkbutton(myWindow, text='Xiao', variable=chooseSecond, onvalue=1, offvalue=0, command=chooseEvent)
checkButton2.place(x=200, y=44)
chooseThird = Tkinter.IntVar()
checkButton3 = Tkinter.Checkbutton(myWindow, text='Hui', variable=chooseThird, onvalue=1, offvalue=0, command=chooseEvent)
checkButton3.place(x=200, y=66)

myWindow.mainloop()
        
    

  运行结果

 

转载于:https://www.cnblogs.com/rainbow-tan/p/11366079.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值