美文网首页
python学习:tkinter接口2-窗口控件

python学习:tkinter接口2-窗口控件

作者: 0清婉0 | 来源:发表于2021-01-16 16:53 被阅读0次

【button按钮控件】

语法

b = tk.Button(window, text='hit me', command=hit_me)

import tkinter as tk

from tkmacosx import Button # 解决在mac下无法修改button背景色

window = tk.Tk()

window.title('My window')

window.geometry('500x300')

var = tk.StringVar()

l = tk.Label(window, textvariable=var, bg='green', fg='white', font={'Arial', 12}, width=30, height=2)

l.pack()

on_hit = False

def hit_me():

    global on_hit

    if on_hit == False:

        on_hit = True

        var.set('you hit me')

    else:

        on_hit = False

        var.set('')

b = Button(window, text='hit me', bg='blue', fg='white', font=('Arial', 12), width=50, height=20, command=hit_me)

b.pack()

window.mainloop()

注意:macOS下python Tkinter中Button无法更改背景颜色,解决方法如下:

1.安装模块:pip3 install tkmacosx

2.导入模块:from tkmacosx import Button

3.按钮:b = Button(window, text='hit me', bg='blue', fg='white', font=('Arial', 12), width=50, height=20, command=hit_me)

【Entry单行文本输入框】

用处:需要用户输入信息时使用

import tkinter as tk

window = tk.Tk()

window.title('My window')

window.geometry('500x300')

e1 = tk.Entry(window, show='*', font=('Arial', 14))  # 密文

e2 = tk.Entry(window, show=None, font=('Arial', 14)) # 明文

e1.pack()

e2.pack()

window.mainloop()

【Text多行文本区域】

import tkinter as tk

from tkmacosx import Button, widget

window = tk.Tk()

window.title('My window')

window.geometry('500x300')

e = tk.Entry(window, show=None)

e.pack()

def insert_point():  # 在鼠标焦点处插入输入内容

    var = e.get()

    t.insert('insert', var)

def insert_end():    # 在文本内容最后接着插入输入内容

    var = e.get()

    t.insert('end', var)

b1 = Button(window, text='insert point', width=100, height=20, command=insert_point)

b1.pack()

b2 = Button(window, text='insert end', width=100, height=20, command=insert_end)

b2.pack()

t = tk.Text(window, height=30)   # 多行

t.pack()

window.mainloop()

【listbox 列表】

import tkinter as tk

from tkmacosx import Button, widget

window = tk.Tk()

window.title('My window')

window.geometry('500x300')

var1 = tk.StringVar()  # 创建变量,鼠标点击具体选项的内容

l = tk.Label(window, bg='green', fg='yellow', font={'Arial', 12}, width=10, textvariable=var1)

l.pack()

# 创建一个函数用于按钮的点击事件

def print_selection():

    value = lb.get(lb.curselection())  # 获取当前选中的文本

    var1.set(value)  # 为label设置值

# 按钮必须放在函数下面,点击按钮调用print_selection函数

b1 = Button(window, text='print selection', width=100, height=20, command=print_selection)

b1.pack()

var2 = tk.StringVar()

var2.set((1,2,3,4))

lb = tk.Listbox(window, listvariable=var2)

# 创建一个列表并将值循环填加到Listbox中

list_items = [11,22,33,44]

for item in list_items:

    lb.insert('end', item)  # 从最后一个位置开始

lb.insert(1, 'first')  # 在第一个位置处增加'first'字符串

lb.insert(2, 'second')

lb.delete(2)

lb.pack()

window.mainloop()

【Radiobutton 单选按钮】macOS下字体颜色有问题,还未找到理想的解决办法

import tkinter as tk

from tkmacosx import Button

window = tk.Tk()

window.title('My window')

window.geometry('500x300')

var = tk.StringVar()

l = tk.Label(window, bg='green', width=20, text='empty')

l.pack()

def print_selection():

    l.config(text='you have selected ' + var.get())

r1 = tk.Radiobutton(window, text='Option A', variable=var, value=1, command=print_selection)

r1.pack()

r2 = tk.Radiobutton(window, text='Option B', variable=var, value=2, command=print_selection)

r2.pack()

r3 = tk.Radiobutton(window, text='Option C', variable=var, value=3, command=print_selection)

r3.pack()

window.mainloop()

相关文章

网友评论

      本文标题:python学习:tkinter接口2-窗口控件

      本文链接:https://www.haomeiwen.com/subject/pkhnaktx.html