20.使用python GUI绘制一个桌面应用,多选内容,有提交按钮和清空按钮
使用python GUI绘制一个桌面应用,多选内容,有提交按钮和清空按钮
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import tkinter as tk 2 3 def show_hobby(): 4 selected_hobbies = [] 5 for hobby, var in hobbies_vars.items(): 6 if var.get(): 7 selected_hobbies.append(hobby) 8 result_label.config(text="你选择的爱好是:" + ", ".join(selected_hobbies)) 9 10 def clear_selection(): 11 for var in hobbies_vars.values(): 12 var.set(False) 13 result_label.config(text="") 14 15 root = tk.Tk() 16 root.title("爱好选择") 17 root.geometry("800x800") # 设置窗口大小为800x800 18 19 # 创建爱好选项 20 hobbies_vars = { 21 "打篮球": tk.BooleanVar(), 22 "打游戏": tk.BooleanVar(), 23 "跑步": tk.BooleanVar(), 24 "读书": tk.BooleanVar(), 25 "旅行": tk.BooleanVar(), 26 "唱歌": tk.BooleanVar(), 27 "画画": tk.BooleanVar(), 28 "编程": tk.BooleanVar() 29 } 30 31 hobbies_frame = tk.Frame(root) 32 hobbies_frame.pack() 33 34 row = 0 35 col = 0 36 max_cols = 6 37 for hobby, var in hobbies_vars.items(): 38 checkbox = tk.Checkbutton(hobbies_frame, text=hobby, variable=var) 39 checkbox.grid(row=row, column=col, sticky="w", padx=10, pady=5) 40 col += 1 41 if col == max_cols: 42 col = 0 43 row += 1 44 45 # 创建提交按钮 46 submit_button = tk.Button(root, text="提交", command=show_hobby) 47 submit_button.pack() 48 49 # 创建清空按钮 50 clear_button = tk.Button(root, text="清空", command=clear_selection) 51 clear_button.pack() 52 53 # 创建结果显示标签 54 result_label = tk.Label(root, text="") 55 result_label.pack() 56 57 root.mainloop()
添加股票K线分析选项
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import tkinter as tk 2 3 def show_hobby(): 4 selected_hobbies = [] 5 for hobby, var in hobbies_vars.items(): 6 if var.get(): 7 selected_hobbies.append(hobby) 8 result_label.config(text="你选择的内容有:" + ", ".join(selected_hobbies)) 9 10 def clear_selection(): 11 for var in hobbies_vars.values(): 12 var.set(False) 13 result_label.config(text="") 14 15 root = tk.Tk() 16 root.title("股票K线分析") 17 root.geometry("800x800") # 设置窗口大小为800x800 18 19 # 创建爱好选项 20 hobbies_vars = { 21 "加速下跌": tk.BooleanVar(), 22 "加速上涨": tk.BooleanVar(), 23 "20日线走空": tk.BooleanVar(), 24 "上不去": tk.BooleanVar(), 25 "掉下来": tk.BooleanVar(), 26 "上升通道": tk.BooleanVar(), 27 "突破": tk.BooleanVar(), 28 "突破5日线": tk.BooleanVar(), 29 "突破20日线": tk.BooleanVar(), 30 "站上20日线": tk.BooleanVar(), 31 "跌破": tk.BooleanVar(), 32 "跌破20日线": tk.BooleanVar(), 33 "破位": tk.BooleanVar(), 34 "顶20日线": tk.BooleanVar(), 35 "受阻": tk.BooleanVar(), 36 "有支撑": tk.BooleanVar(), 37 "顶缺口": tk.BooleanVar(), 38 "缺口支撑": tk.BooleanVar(), 39 "补缺口": tk.BooleanVar(), 40 "缺口补掉": tk.BooleanVar(), 41 "缺口阻力": tk.BooleanVar(), 42 "有腿": tk.BooleanVar(), 43 "阳胜阴": tk.BooleanVar(), 44 "空翻多": tk.BooleanVar(), 45 "尾盘翘尾": tk.BooleanVar(), 46 "多方炮": tk.BooleanVar(), 47 "空方炮": tk.BooleanVar(), 48 "老鸭头": tk.BooleanVar(), 49 "反包": tk.BooleanVar(), 50 "吞没": tk.BooleanVar(), 51 "回踩": tk.BooleanVar(), 52 "震荡": tk.BooleanVar(), 53 "单阳不破": tk.BooleanVar(), 54 "尾盘上板": tk.BooleanVar(), 55 "烂板": tk.BooleanVar(), 56 "回到原点": tk.BooleanVar(), 57 "前低": tk.BooleanVar(), 58 "三角形": tk.BooleanVar(), 59 "一阴穿三线": tk.BooleanVar(), 60 "断头铡刀": tk.BooleanVar(), 61 "仙人指路": tk.BooleanVar(), 62 "等阳线反包": tk.BooleanVar(), 63 "底部起量": tk.BooleanVar(), 64 "放巨量": tk.BooleanVar(), 65 "缩量": tk.BooleanVar(), 66 "异常量": tk.BooleanVar(), 67 "诱多": tk.BooleanVar(), 68 "选方向": tk.BooleanVar(), 69 "看空": tk.BooleanVar(), 70 "盘整": tk.BooleanVar(), 71 "试盘": tk.BooleanVar(), 72 "该飞不飞": tk.BooleanVar(), 73 "止盈": tk.BooleanVar(), 74 "止损": tk.BooleanVar(), 75 "目标位": tk.BooleanVar(), 76 "直接干到目标位": tk.BooleanVar() 77 } 78 79 hobbies_frame = tk.Frame(root) 80 hobbies_frame.pack() 81 82 row = 0 83 col = 0 84 max_cols = 6 85 for hobby, var in hobbies_vars.items(): 86 checkbox = tk.Checkbutton(hobbies_frame, text=hobby, variable=var) 87 checkbox.grid(row=row, column=col, sticky="w", padx=10, pady=5) 88 col += 1 89 if col == max_cols: 90 col = 0 91 row += 1 92 93 # 创建提交按钮 94 submit_button = tk.Button(root, text="提交", command=show_hobby) 95 submit_button.pack() 96 97 # 创建清空按钮 98 clear_button = tk.Button(root, text="清空", command=clear_selection) 99 clear_button.pack() 100 101 # 创建结果显示标签 102 result_label = tk.Label(root, text="") 103 result_label.pack() 104 105 root.mainloop()