将可变变量设置到菜单栏条目中

时间:2019-01-04 15:04:25

标签: python python-3.x tkinter

我目前正在为程序使用图形用户界面,我想在显示 IntVar 的菜单栏中(在我的情况下,在menubaroptions方法中)创建一个Entry小部件。设置为某个数字(在我的情况下为9),但可由用户更改。在我的代码中,我使用self.entrystring.get()进行了尝试,但收到了“ self is not defined”错误。

这是我的代码的一部分:

import tkinter
from tkinter.constants import *
from tkinter import messagebox
from struct import unpack
from codecs import decode


class Graphicaluserinterface(tkinter.Frame):

    @classmethod
    def main(cls):
        root = tkinter.Tk()
        root.title('Program')
        root.minsize(560, 105)
        gui = cls(root)
        gui.grid(row=0, column=0, sticky=NSEW)
        root.grid_rowconfigure(0, weight=1)
        root.grid_columnconfigure(0, weight=1)
        root['menu'] = gui.menubar
        root.mainloop()

    def __init__(self, master=None):
        super().__init__(master)
        self.inputliste = []
        self.check1 = tkinter.IntVar()
        self.check2 = tkinter.IntVar()
        self.check3 = tkinter.IntVar()
        self.check5 = tkinter.IntVar()
        self.inputfilenamelist = []
        self.fileopenname = tkinter.StringVar()
        self.fileopenname1 = tkinter.StringVar()
        self.filesavename  =tkinter.StringVar()
        self.entrystring = tkinter.IntVar()
        self.taktzykluszeit = tkinter.DoubleVar()
        self.taktunterschiedboolean = tkinter.BooleanVar()
        self.fileopeningcounter = tkinter.IntVar()
        self.fileopeningcounter.set(0)
        self.menubar = tkinter.Menu(self)
        self.file_menu = tkinter.Menu(self.menubar, tearoff=FALSE)
        self.help_menu = tkinter.Menu(self.menubar, tearoff=FALSE)
        self.program_start = tkinter.Button(self, text='Start Program')
        self.check_button1 = tkinter.Checkbutton(
        self, text="Drehzahl und Drehmoment", variable=self.check1,
        onvalue=1, offvalue=0
    )
        self.check_button2 = tkinter.Checkbutton(
        self, text="Analogvoltsensoren", variable=self.check2,
        onvalue=1, offvalue=0
    )
        self.check_button3 = tkinter.Checkbutton(
        self, text="Analogamperesensoren", variable=self.check3,
        onvalue=1, offvalue=0
    )
        self.check_button4 = tkinter.Checkbutton(
        self, text="Thermoelemente", variable=self.check4,
        onvalue=1, offvalue=0
    )
        self.check_button5 = tkinter.Checkbutton(
        self, text="Pt-100-Elemente", variable=self.check5,
        onvalue=1, offvalue=0)
        self.input_path_display = tkinter.Label(
        self, textvariable=self.fileopenname1, bg='white', width=60
    )
        self.output_path_display = tkinter.Label(
        self, textvariable=self.filesavename, bg="white", width=60
    )
        self.input_path_display_label = tkinter.Label(self, text="Inputfile")
        self.output_path_display_label = tkinter.Label(self, text="Outputfile")
        self.create_widgets()
        self.entrystring.set(9)
        self.taktzykluszeit.set(0.0)
        self.taktunterschiedboolean.set(False)

    def create_widgets(self):
        self.menubar.add_cascade(label="File", menu=self.file_menu)
        self.file_menu.add_command(label="Open", command=lambda:[self.inputfilenamelist.clear(),self.fileopening()])
        self.file_menu.add_command(label="Save As")
        self.file_menu.add_command(label="Options",command=self.menubaroptions)
        self.file_menu.add_command(label="Exit", command=self.master.destroy)
        self.menubar.add_cascade(label="Extras", menu=self.help_menu)
        self.help_menu.add_command(label="Help")
        self.help_menu.add_command(label="Credits")
        pad = dict(padx=5, pady=5)
        self.check_button1.grid(row=0, column=0, **pad)
        self.check_button2.grid(row=1, column=0, **pad)
        self.check_button3.grid(row=2, column=0, **pad)
        self.check_button4.grid(row=3, column=0, **pad)
        self.check_button5.grid(row=4, column=0, **pad)
        self.input_path_display_label.grid(row=0, column=1, sticky=EW, **pad)
        self.input_path_display.grid(row=1, column=1, sticky=NSEW, **pad)
        self.output_path_display_label.grid(row=2, column=1, sticky=EW, **pad)
        self.output_path_display.grid(row=3, column=1, sticky=NSEW, **pad)
        self.program_start.grid(row=4, column=1, sticky=EW, **pad)
        #self.program_start["command"]=lambda:[self.fileselectwarning(),self.writealldatafile(),self.writeselecteddata(),
        #                                     self.inputliste.clear(),self.fileopeningcounter.set(0),
        #                                     self.inputfilenamelist.clear()]
        self.grid_rowconfigure(1, weight=1)
        self.grid_columnconfigure(1, weight=1)

    def menubaroptions(root):
        optionswindow = tkinter.Toplevel(root)
        optionswindow.title("Options")
        optionswindow.minsize(300,150)
        trennzeichenlabel = tkinter.Label(optionswindow,text="Length of Separator in Byte:").pack()
        trennzeichenentry = tkinter.Entry(optionswindow,textvariable=self.entrystring.get(),width=30,justify="center").pack()
        taktzykluszeitlabel = tkinter.Label(optionswindow,text="Measurementtime for all \n Temperature-Sensors in sec").pack()
        taktzykluszeitentry = tkinter.Entry(optionswindow,textvariable=self.taktzykluszeit.get(),width=30,justify="center").pack()


if __name__ == '__main__':
    Graphicaluserinterface.main()

我知道应该缩进一行,但是它在这里行不通,尽管我在代码中将其缩进了。

1 个答案:

答案 0 :(得分:1)

有几个问题:

在函数menubaroptions()中,应在分配给对象时将textvariable分配给IntVar.get()方法:

trennzeichenentry = tkinter.Entry( ... textvariable=self.entrystring.get(), ...).pack()

应为:

trennzeichenentry = tkinter.Entry( ... textvariable=self.entrystring, ...).pack()

然后,您使用实例名称root而不是self定义函数,这意味着self.entrystring将生成NameError。

然后,您尝试创建一个Toplevel窗口作为root的子级。但是rootmain()函数中的局部变量,menubaroptions()找不到它。

现在;您正在使用装饰器@classmethod,但我还没有装饰器,所以我不能说这是否会影响问题。但是我上面提到的事情会让您有所作为。