[英]How to display the variable in a tkinter window?
我對Python完全陌生,我正在研究以下代碼,這些代碼是在stackflow用戶的大力幫助下編寫的。
運行腳本后,將打開一個tkinter窗口,您可以在其中選擇一個gcode文件(該文件包含3D打印機的許多指令行),然后從該文件中找到一個特定值。
我想實現的目標是:
1)在tkinter窗口的“加載GCODE”按鈕下顯示此值,並提供說明/標簽。
2)對此值進行一些計算,並將其顯示在tkinter窗口中。
3)最后使該腳本成為可執行文件,以便每個人都可以使用它,即使沒有安裝Python。
我不確定這是否超級簡單,還是需要大量工作,因為我是Python的新手(在整體編程上不太好)。 希望我已經講得足夠好了,並在此先感謝您的投入!
用於代碼測試的Gcode文件 : GCODE FILE
完成代碼:
from tkinter import *
import re
from tkinter import messagebox
from tkinter import filedialog
# Here, we are creating our class, Window, and inheriting from the Frame
# class. Frame is a class from the tkinter module. (see Lib/tkinter/__init__)
class Window(Frame):
# Define settings upon initialization. Here you can specify
def __init__(self, master=None):
# parameters that you want to send through the Frame class.
Frame.__init__(self, master)
#reference to the master widget, which is the tk window
self.master = master
#with that, we want to then run init_window, which doesn't yet exist
self.init_window()
# Load the gcode file in and extract the filament value
def get_filament_value(self, fileName):
with open(fileName, 'r') as 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 the print is {} cm3'.format(value))
else:
value = 0.0
return('Filament volume was 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 by Bartosz Domagalski to find used filament parameters from Sli3er generated GCODE")
#Creation of init_window
def init_window(self):
# changing the title of our master widget
self.master.title("Filament Data")
# allowing the widget to take the full space of the root window
self.pack(fill=BOTH, expand=1)
# creating a menu instance
menu = Menu(self.master)
self.master.config(menu=menu)
# create the file object)
file = Menu(menu)
help = Menu(menu)
# adds a command to the menu option, calling it exit, and the
# command it runs on event is client_exit
file.add_command(label="Exit", command=self.client_exit)
help.add_command(label="About", command=self.about_popup)
#added "file" to our menu
menu.add_cascade(label="File", menu=file)
menu.add_cascade(label="Help", menu=help)
#Creating the labels
self.value = StringVar()
l_instruction = Label(self, justify=CENTER, compound=TOP, text="Load GCODE file to find volume, \n weight and 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 the 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 for file...", bd=1, relief=SUNKEN, anchor=W)
status.pack(side=BOTTOM, fill=X)
# root window created. Here, that would be the only window, but you can later have windows within windows.
root = Tk()
root.resizable(width=False,height=False);
root.geometry("220x300")
#creation of an instance
app = Window(root)
#mainloop
root.mainloop()
您應該將Label(s)
放置在與按鈕相同的Frame
中。 當我在您的代碼中復制時,我也必須導入filedalog
模塊( from tkinter import filedalog
),星號導入似乎並沒有覆蓋它。
您可以使用StringVar()
變量(來自tkinter)並將其分配給標簽。 您可以從中獲取.set()
和.get()
值。 創建變量並將其分配給Label:
self.value = StringVar('', value=" Load GCODE file to find volume, \n weight and price of used filament.")
l = Label(self, textvariable=self.value)
更改變量值:
self.value.set(self.get_filament_value(root.fileName))
您必須根據需要放置Label,就像使用quitButton.place
的Button quitButton.place
。 還有其他方法可以處理布局,網格和打包。 但據我所知,建議為所有元素選擇一種布局樣式。
創建可執行文件,“凍結”您的代碼,是更廣泛的主題。 在這里查看一些可以進一步了解的選項。
編輯:
更新了工作代碼。 改變小部件的位置,您會發現的;)沒有查看代碼中的任何新元素。
from tkinter import *
import re
from tkinter import messagebox, filedialog
# Here, we are creating our class, Window, and inheriting from the Frame
# class. Frame is a class from the tkinter module. (see Lib/tkinter/__init__)
class Window(Frame):
# Define settings upon initialization. Here you can specify
def __init__(self, master=None):
# parameters that you want to send through the Frame class.
Frame.__init__(self, master)
# reference to the master widget, which is the tk window
self.master = master
# with that, we want to then run init_window, which doesn't yet exist
self.init_window()
# Load the gcode file in and extract the filament value
def get_filament_value(self, fileName):
with open(fileName, 'r') as 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 the print is {} cm3'.format(value)
else:
return 'Filament volume was 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 by Bartosz Domagalski to find used filament parameters from Sli3er generated GCODE")
# Creation of init_window
def init_window(self):
# changing the title of our master widget
self.master.title("Filament Data")
# allowing the widget to take the full space of the root window
self.pack(fill=BOTH, expand=1)
# creating a menu instance
menu = Menu(self.master)
self.master.config(menu=menu)
# create the file object)
file = Menu(menu)
help = Menu(menu)
# adds a command to the menu option, calling it exit, and the
# command it runs on event is client_exit
file.add_command(label="Exit", command=self.client_exit)
help.add_command(label="About", command=self.about_popup)
# added "file" to our menu
menu.add_cascade(label="File", menu=file)
menu.add_cascade(label="Help", menu=help)
# Creating the labels
self.value = StringVar()
l_instruction = Label(self, justify=CENTER, compound=TOP,
text=" Load GCODE file to find volume, \n weight and 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 the 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 for file...", bd=1, relief=SUNKEN, anchor=W)
status.pack(side=BOTTOM, fill=X)
# root window created. Here, that would be the only window, but you can later have windows within windows.
root = Tk()
root.resizable(width=False, height=False);
# root.geometry("400x300")
# creation of an instance
app = Window(root)
# mainloop
root.mainloop()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.