Python/Tkinter新手求助:GUI按钮与单选框联动及图片刷新实现
Tkinter GUI开发需求及当前实现
需求说明
- 具备Python基础,但无GUI开发经验,正在使用Tkinter开发一款包含4个Frame的GUI程序
- 左上Frame:固定显示参考图
- 左下Frame:包含三类单选按钮:
- 故障类型(Fault Type)
- 在线DER数量(# of DERs On)
- 现有或规划光伏(Existing or Projected PV)
- 核心功能:点击红色「RUN!」按钮后,根据单选框的选择,在右上、右下Frame分别加载对应的图片,且支持多次选择刷新图片
当前代码
from tkinter import * from PIL import Image, ImageTk root = Tk() root.title("Distribution Feeder Schematic") # title of the GUI window root.minsize(500, 500) # width, height root.maxsize(1500, 1000) # specify the max size the window can expand to root.config(bg="skyblue") # specify background color window_width = 1500 window_height = 1000 # get the screen dimension screen_width = root.winfo_screenwidth() screen_height = root.winfo_screenheight() # find the center point center_x = int(screen_width/2 - window_width / 2) center_y = int(screen_height/2 - window_height / 2) # set the position of the window to the center of the screen root.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}') # Create top/bottom left, top right, and bottom right frames top_left_frame = Frame(root, width=740, height=770, bg='white') top_left_frame.place(x=5, y=0) bot_left_frame = Frame(root, width=740, height=220, bg='grey') bot_left_frame.place(x=5, y=775) top_right_frame = Frame(root, width=745, height=490, bg='grey') top_right_frame.place(x=750, y=0) bot_right_frame = Frame(root, width=745, height=490, bg='grey') bot_right_frame.place(x=750, y=500) # Create frames and labels in left_frame Label(top_left_frame, text="Feeder Overview", font=("Calibri, 15"), bg= 'white').place(x=320, y=0) # load image to be "edited" feeder = Image.open("Feeder_ScreenShot.png") image2 = feeder.resize((740, 770), Image.ANTIALIAS) feeder2 = ImageTk.PhotoImage(image2) Label(top_left_frame, image=feeder2).place(x=0, y=25) Label(bot_left_frame, text="Simulation Parameters: Choose a Fault Type, # of DERs Online, and Existing or Projected PV online. Then Click RUN!").place(x=15, y=0) L1 = Label(top_left_frame, text='<-- Fault\nLocation', bg = "white", fg = "green", font = ("Arial",28)) L1.place(x=400, y=700) Fault_Type = Label(bot_left_frame, text = "Fault Type:", bg = "white", fg = "orange", font = ("Arial",18)) Fault_Type.place(x=5,y=35) Number_DERs = Label(bot_left_frame, text = "# of DERs On:", bg = "white", fg = "orange", font = ("Arial",18)) Number_DERs.place(x=150,y=35) PV = Label(bot_left_frame, text = "Existing or Projected PV:", bg = "white", fg = "orange", font = ("Arial",18)) PV.place(x=330,y=35) def retrieve(): print(Var1.get()) Var1 = IntVar() Var2 = IntVar() Var3 = IntVar() Nada = Radiobutton(bot_left_frame, text = "None", variable = Var1, value = 0, command = retrieve) Nada.place(x=5, y=75) SLG = Radiobutton(bot_left_frame, text = "Single-Line-\nTo-Ground", variable = Var1, value = 1, command = retrieve) SLG.place(x=5, y=110) ThreePh = Radiobutton(bot_left_frame, text = "3-Phase", variable = Var1, value = 2, command = retrieve) ThreePh.place(x=5, y=160) ZeroDER = Radiobutton(bot_left_frame, text = "0", variable = Var2, value = 0, command = retrieve) ZeroDER.place(x=150, y=75) OneDER = Radiobutton(bot_left_frame, text = "1", variable = Var2, value = 1, command = retrieve) OneDER.place(x=150, y=110) TwoDER = Radiobutton(bot_left_frame, text = "2", variable = Var2, value = 2, command = retrieve) TwoDER.place(x=150, y=145) ThreeDER = Radiobutton(bot_left_frame, text = "3", variable = Var2, value = 3, command = retrieve) ThreeDER.place(x=150, y=180) FourDER = Radiobutton(bot_left_frame, text = "4", variable = Var2, value = 4, command = retrieve) FourDER.place(x=200, y=75) FiveDER = Radiobutton(bot_left_frame, text = "5", variable = Var2, value = 5, command = retrieve) FiveDER.place(x=200, y=110) SixDER = Radiobutton(bot_left_frame, text = "6", variable = Var2, value = 6, command = retrieve) SixDER.place(x=200, y=145) Existing_PV = Radiobutton(bot_left_frame, text = "Existing PV", variable = Var3, value = 0, command = retrieve) Existing_PV.place(x=330, y=75) Proj_PV = Radiobutton(bot_left_frame, text = "Projected \n(Future) PV", variable = Var3, value = 1, command = retrieve) Proj_PV.place(x=330, y=110) Run = Button(bot_left_frame, text = "RUN!", bg="white", fg="red", font=("Arial",26), command = retrieve) Run.place(x=600, y=105) root.mainloop()
GUI界面截图

内容的提问来源于stack exchange,提问作者thenickmeister
相关产品推荐
相关产品推荐

