Как отобразить переменную в окне tkinter?

Я совершенно новичок в Python и работаю над следующим кодом, который я сделал с большой помощью такого же пользователя стека потока.

После запуска сценария откроется окно tkinter, где вы можете выбрать файл gcode (это файл с множеством строк инструкций для 3D-принтера), а затем будет найдено определенное значение из этого файла.

Чего я хотел бы достичь, так это:

1) Отобразите это значение под кнопкой загрузки GCODE в окне tkinter с описанием / меткой.

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()

1 ответ

Решение

Вы должны поставить Label(s) внутри того же Frame как у вас есть кнопка. И когда я скопировал в ваш код, мне пришлось импортировать filedalog модуль также (from tkinter import filedalog), импорт звездочек, кажется, не покрывает это.

Вы могли бы использовать StringVar() переменной (от tkinter) и назначьте ее метке. Эта переменная вы можете .set() а также .get() значения от. Создайте переменную и назначьте ее метке:

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))

Метка, которую вы должны будете разместить, как вы хотите, так же, как вы сделали с кнопкой с 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()
Другие вопросы по тегам