user interface - How to make expressions in Tkinter Python -
i'm new in python programming , i'm having issues in developing specific part of gui tkinter.
what i'm trying is, space user enter (type) math equation , software make calculation variables calculated.
i've found lot of calculators tkinter, none of i'm looking for. , don't have experience classes definitions.
i made simple layout explain better want do:
import tkinter tk root = tk.tk() iflabel = tk.label(root, text = "if...") iflabel.pack() ifentry = tk.entry(root) ifentry.pack() thenlabel = tk.label(root, text = "then...") thenentry = tk.entry(root) thenlabel.pack() thenentry.pack() elselabel = tk.label(root, text = "else..") elseentry = tk.entry(root) elselabel.pack() elseentry.pack() applybutton = tk.button(root, text = "calculate") applybutton.pack() root.mainloop()
this simple code python 3 have 3 entry spaces
1st) if...
2nd then...
3rd) else...
so, user enter conditional expression , software job. in mind, important thing if user left "if" space in blank, type expression inside "then..." entry , press button "calculate" or build expression statements.
if give ideas how , do....
(without classes, if possible)
i'l give situations exemplification 1st using statements:
var = variable calculated , stored in script out = output if var >= 10 out = 4 else out = 2
2nd without using statement user type in "then" entry expression want calculate , be:
then: out = (((var)**2) +(2*var))**(1/2)
again, it's exemplification...i don't need specific layout. if has idea how construct better, welcome.
thanks all.
here simple version of trying do.
we need use eval
built in function evaluate math of string.
we should write our code error handling there change user type formula wrong , eval
statement fail.
for more information on eval
, exec
take @ post here. think job of explaining two.
here like:
import tkinter tk root = tk.tk() math_label = tk.label(root, text = "type formula , press calculate button.") math_label.pack() math_entry = tk.entry(root) math_entry.pack() result_label = tk.label(root, text = "results: ") result_label.pack(side = "bottom") def perform_calc(): global result_label try: result = eval(math_entry.get()) result_label.config(text = "results: {}".format(result)) except: result_label.config(text = "bad formula, try again.") applybutton = tk.button(root, text = "calculate", command = perform_calc) applybutton.pack() root.mainloop()
Comments
Post a Comment