20-Tkinter 组件-单选框/下拉框/多选框实例

单选框/下拉框/多选框实例

import tkinter as tk
from tkinter import ttk
from tkinter import messagebox

# 创建一个窗口实例
window = tk.Tk()

# 设置窗口标题
window.title('my_window')

# 设置窗口大小和位置(宽度 x 高度 + x偏移 + y偏移)
window.geometry('500x300')

# 输入姓名
tk.Label(window, text='姓名: ').place(x=20, y=20)
entry_name = tk.Entry(window).place(x=100, y=20)

# 选择性别
tk.Label(window, text='性别: ').place(x=20, y=50)
gender = tk.IntVar()
# 其中当我们选中了其中一个选项,把 value 的值 '男' 放到变量 gender 中
rad_gender_1 = tk.Radiobutton(window, text='男', variable=gender, value=1)
rad_gender_2 = tk.Radiobutton(window, text='女', variable=gender, value=2)
rad_gender_1.place(x=100, y=50)
rad_gender_2.place(x=150, y=50)

# 选择出生年月
tk.Label(window, text='出生年月: ').place(x=20, y=80)
tk.Label(window, text='年 ').place(x=180, y=80)
tk.Label(window, text='月 ').place(x=290, y=80)

birth_year = tk.StringVar()
combo_birth_year = ttk.Combobox(window, width=8, textvariable=birth_year)
combo_birth_year['value'] = [str(i) for i in range(1950, 2021)]
combo_birth_year.place(x=100, y=80)

birth_mon = tk.StringVar()
combo_birth_mon = ttk.Combobox(window, width=8, textvariable=birth_mon)
combo_birth_mon['value'] = [str(i) for i in range(1, 13)]
combo_birth_mon.place(x=210, y=80)

# 选择爱好
tk.Label(window, text='爱好: ').place(x=20, y=110)
hobbys = {0: '唱歌', 1: '跳舞', 2: '篮球', 3: '足球', 4: '绘画'}
dic_hobby = {}

for i in range(len(hobbys)):
    dic_hobby[i] = tk.BooleanVar()
    cbtn_hobby = tk.Checkbutton(window, text=hobbys[i], variable=dic_hobby[i])
    cbtn_hobby.place(x=100 + i * 60, y=110)

# 主窗口循环显示
window.mainloop()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

士别三日,当挖目相待

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

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

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

打赏作者

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

抵扣说明:

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

余额充值