python - How to display the variable in a tkinter window? -


i new python , working on following code made big of fellow stackflow user.

after running script tkinter window open can select gcode file (it file many many lines of instructions 3d printer) , later specific value file found.

what achieve to:

1) display value under load gcode button in tkinter window description/label.

2) make calculations on value , display them in tkinter window too.

3) finaly make executable of script can use it, without python installed.

i not sure if super easy or it's lot of work new python (and not in programming overall). hope have explained things enough , thank in advance input!

gcode file code testing: gcode file

finaly code:

from tkinter import * import re tkinter import messagebox tkinter import filedialog  # here, creating our class, window, , inheriting frame # class. frame class tkinter module. (see lib/tkinter/__init__) class window(frame):      # define settings upon initialization. here can specify     def __init__(self, master=none):          # parameters want send through frame class.          frame.__init__(self, master)             #reference master widget, tk window                          self.master = master          #with that, want run init_window, doesn't yet exist         self.init_window()      # load gcode file in , extract filament value     def get_filament_value(self, filename):         open(filename, 'r') f_gcode:             data = f_gcode.read()             re_value = re.search('filament used = .*? \(([0-9.]+)', data)              if re_value:                 value = float(re_value.group(1))                 return('volume of print {} cm3'.format(value))             else:                 value = 0.0                 return('filament volume not found in {}'.format(filename))         return value      def read_gcode(self):         root.filename = filedialog.askopenfilename(filetypes = (("gcode files", "*.gcode"), ("all files", "*.*")))         self.value.set = self.get_filament_value(root.filename) #       self.value.set('button pressed')      def client_exit(self):         exit()      def about_popup(self):         messagebox.showinfo("about", "small software created bartosz domagalski find used filament parameters sli3er generated gcode")      #creation of init_window     def init_window(self):          # changing title of our master widget               self.master.title("filament data")          # allowing widget take full space of root window         self.pack(fill=both, expand=1)          # creating menu instance         menu = menu(self.master)         self.master.config(menu=menu)          # create file object)         file = menu(menu)         = menu(menu)          # adds command menu option, calling exit, ,         # command runs on event client_exit         file.add_command(label="exit", command=self.client_exit)         help.add_command(label="about", command=self.about_popup)          #added "file" our menu         menu.add_cascade(label="file", menu=file)         menu.add_cascade(label="help", menu=help)           #creating labels         self.value = stringvar()         l_instruction = label(self, justify=center, compound=top, text="load gcode file find volume, \n weight , price of used filament.")         l = label(self, justify=center, compound=bottom, textvariable=self.value) #       l.place(x=85, y=45)         l_instruction.pack()         l.pack()          #creating button         gcodebutton = button(self, text="load gcode", command=self.read_gcode)         gcodebutton.pack() #       gcodebutton.place(x=140, y=10)          #status bar         status = label(self, text="waiting file...", bd=1, relief=sunken, anchor=w)         status.pack(side=bottom, fill=x)  # root window created. here, window, can later have windows within windows. root = tk() root.resizable(width=false,height=false); root.geometry("220x300")   #creation of instance app = window(root)  #mainloop  root.mainloop() 

you should put label(s) inside same frame have button. , when copied in code had import filedalog module (from tkinter import filedalog), asterisk import didn't seem cover it.

you use stringvar() variable (from tkinter) , assign label. variable can .set() , .get() values from. create variable , assign label:

self.value = stringvar('', value="  load gcode file find volume, \n weight , price of used filament.") l = label(self, textvariable=self.value) 

alter variable value:

self.value.set(self.get_filament_value(root.filename)) 

the label have place want it, did button quitbutton.place. there other ways handle layout, grid , pack. far know, recommended pick 1 layout style elements.

creating executable, "freezing" code, broader topic. take look here options can more into.


edit:

updated working code. changing placement of widgets you'll figure out ;) not looked @ new elements in code.

from tkinter import * import re tkinter import messagebox, filedialog   # here, creating our class, window, , inheriting frame # class. frame class tkinter module. (see lib/tkinter/__init__) class window(frame):     # define settings upon initialization. here can specify     def __init__(self, master=none):          # parameters want send through frame class.         frame.__init__(self, master)          # reference master widget, tk window         self.master = master          # that, want run init_window, doesn't yet exist         self.init_window()      # load gcode file in , extract filament value     def get_filament_value(self, filename):         open(filename, 'r') f_gcode:             data = f_gcode.read()             re_value = re.search('filament used = .*? \(([0-9.]+)', data)              if re_value:                 value = float(re_value.group(1))                 return 'volume of print {} cm3'.format(value)             else:                 return 'filament volume not found in {}'.format(filename)      def read_gcode(self):         root.filename = filedialog.askopenfilename(filetypes=(("gcode files", "*.gcode"), ("all files", "*.*")))         self.value.set(self.get_filament_value(root.filename))      def client_exit(self):         exit()      def about_popup(self):         messagebox.showinfo("about",                             "small software created bartosz domagalski find used filament parameters sli3er generated gcode")      # creation of init_window     def init_window(self):          # changing title of our master widget         self.master.title("filament data")          # allowing widget take full space of root window         self.pack(fill=both, expand=1)          # creating menu instance         menu = menu(self.master)         self.master.config(menu=menu)          # create file object)         file = menu(menu)         = menu(menu)          # adds command menu option, calling exit, ,         # command runs on event client_exit         file.add_command(label="exit", command=self.client_exit)         help.add_command(label="about", command=self.about_popup)          # added "file" our menu         menu.add_cascade(label="file", menu=file)         menu.add_cascade(label="help", menu=help)          # creating labels         self.value = stringvar()         l_instruction = label(self, justify=center, compound=top,                               text="  load gcode file find volume, \n weight , price of used filament.")         l = label(self, justify=center, compound=bottom, textvariable=self.value)         #       l.place(x=85, y=45)         l_instruction.pack()         l.pack()          l_instruction.pack()         self.value = stringvar()         l = label(self, textvariable=self.value)         l.pack()          # creating button         gcodebutton = button(self, text="load gcode", command=self.read_gcode)         gcodebutton.pack()         #       gcodebutton.place(x=140, y=10)          # status bar         status = label(self, text="waiting file...", bd=1, relief=sunken, anchor=w)         status.pack(side=bottom, fill=x)   # root window created. here, window, can later have windows within windows. root = tk() root.resizable(width=false, height=false); # root.geometry("400x300")   # creation of instance app = window(root)  # mainloop root.mainloop() 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -