Sep-30-2021, 07:51 AM
(This post was last modified: Sep-30-2021, 07:03 PM by Yoriz.
Edit Reason: Added prefix
)
Hallo everyone, I have the following code. The goal is to create a GUI that takes the entry values and update vriables value that I want to use lter in another script that I have already tested. At this stage I am simply printing the values from a button (RUN SIMULATION). It works but I have the following issues:
- when I start the GUI the predefined value in the entry are like
.!frame2.!entry
- to print the entry value I need to call each dictionary item with the method
get()(button RUN SIMULATION). Can I save directly the entry values in the dictianary without the need to use such a method in the button?
def create_geometry_input_dictionary(frame, var):
temp = {'TITLE': ['Min', 'Step', 'Max'],
'LABEL1': [StringVar(frame, value='25'),
StringVar(frame, value=var),
StringVar(frame, value=var)],
'LABEL2': [StringVar(frame, value='19'),
StringVar(frame, value=var),
StringVar(frame, value=var)],
'LABEL3': [StringVar(frame, value='0.8'),
StringVar(frame, value=var),
StringVar(frame, value=var)],
'LABEL4': [StringVar(frame, value='2'),
StringVar(frame, value=var),
StringVar(frame, value=var)],
'LABEL5': [StringVar(frame, value='0.1'),
StringVar(frame, value=var),
StringVar(frame, value=var)]}
return temp
def create_fields(frame, fields_dict):
for ind, key in enumerate(fields_dict):
lab_temp = Label(frame, text=key)
lab_temp.grid(row=ind, column=0, sticky=NS, padx=5, pady=5)
for i, item in enumerate(fields_dict[key]):
if ind == 0:
lab_temp = Label(frame, text=item)
lab_temp.grid(row=ind, column=i + 1, sticky=NS, padx=5, pady=5)
else:
val = Entry(frame, textvariable=item, width=15)
val.grid(row=ind, column=i + 1, sticky="ns", padx=5, pady=5)
fields_dict[key][i].set(val)
if __name__ == '__main__':
root = Tk()
root.title('Model Definition')
width = 980
height = 40
root.geometry('{}x{}'.format(width, 10 * height))
width_label = 30
# create all of the main containers
top_frame = Frame(root, width=width, height=40, pady=3)
input_frame_left = Frame(root, width=width / 2, height=40, pady=3)
input_frame_right = Frame(root, width=width / 2, height=40, pady=3)
frame_file = Frame(root, width=width, height=40, pady=3) # include file path
output_frame_left = Frame(root, width=width / 2, height=40, pady=3)
output_frame_right = Frame(root, width=width / 2, height=40, pady=3)
button_frame = Frame(root, width=width, height=40, pady=3)
# place the container within the main window using grid
top_frame.grid(row=0, columnspan=2, sticky="n")
input_frame_left.grid(row=1, column=0, sticky="nw")
input_frame_right.grid(row=1, column=1, sticky="ne")
frame_file.grid(row=2, columnspan=2, sticky="ns")
output_frame_left.grid(row=3, column=0, sticky="w")
output_frame_right.grid(row=3, column=1, sticky="e")
button_frame.grid(row=4, columnspan=2, sticky="n")
# create radio button TYPE of ANALYSIS
label_top_frame = Label(top_frame, text='TYPE OF ANALYSIS', bd=5, anchor=N).grid(row=0, columnspan=2, sticky=N)
type_analysis = StringVar(value='1')
Radiobutton(top_frame, text='One-side heating', value='1', variable=type_analysis).grid(row=1, column=0, sticky=N)
Radiobutton(top_frame, text='Uniform heating', value='2', variable=type_analysis).grid(row=1, column=1, sticky=N)
lab = Label(input_frame_left, text='GEOMETRY DATA')
lab.grid(row=0, column=0, sticky=N, padx=5, pady=5)
lab = Label(input_frame_left, text='Max')
lab.grid(row=0, column=1, sticky=N, padx=5, pady=5)
lab = Label(input_frame_left, text='Step')
lab.grid(row=0, column=2, sticky=N, padx=5, pady=5)
lab = Label(input_frame_left, text='Min')
lab.grid(row=0, column=3, sticky=N, padx=5, pady=5)
dict_input_geometry = create_geometry_input_dictionary(input_frame_left, '0')
dict_input_water = create_water_input_dictionary(input_frame_right, '0')
create_fields(input_frame_left, dict_input_geometry)
create_fields(input_frame_right, dict_input_water)
input_path = StringVar(value=path_temp)
Label(frame_file, text='path').grid(row=0, columnspan=2)
Label(frame_file, textvariable=input_path).grid(row=1, column=0, sticky='w') # textvariable=input_path.get() does not work, why?
Button(frame_file, text='Open file', command=lambda: load_file(input_path)).grid(row=1, column=1)
Button(frame_file, text='Run simulation', command=lambda: print(dict_input_geometry['LABEL1'][0].get())).grid(row=1, column=2)
