Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将可变变量设置为菜单栏条目_Python_Python 3.x_Tkinter - Fatal编程技术网

Python 将可变变量设置为菜单栏条目

Python 将可变变量设置为菜单栏条目,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我目前正在为我的程序开发图形用户界面,我想在菜单栏中创建一个条目小部件(在我的例子中是menubaroptions方法),它显示一个设置为特定数字(在我的例子中是9)但可由用户更改的IntVar。在我的代码中,我使用self.entrystring.get()尝试了它,但得到了“self未定义”错误 这是我代码的一部分: import tkinter from tkinter.constants import * from tkinter import messagebox from struc

我目前正在为我的程序开发图形用户界面,我想在菜单栏中创建一个条目小部件(在我的例子中是
menubaroptions
方法),它显示一个设置为特定数字(在我的例子中是9)但可由用户更改的IntVar。在我的代码中,我使用self.entrystring.get()尝试了它,但得到了“self未定义”错误

这是我代码的一部分:

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

我知道有一行应该缩进,但它在这里不起作用,但我在代码中缩进了它。

有几个问题:

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

trennzeichenentry = tkinter.Entry( ... textvariable=self.entrystring.get(), ...).pack()
应该是:

trennzeichenentry = tkinter.Entry( ... textvariable=self.entrystring, ...).pack()
然后用实例名
root
而不是
self
定义函数,这意味着
self.entrystring
将生成一个NameError

然后尝试创建一个顶级窗口作为
根目录的子窗口。但是
root
main()
函数中的一个局部变量,
menubaroptions()
无法找到它


现在;您正在使用decorator
@classmethod
,而我还没有使用decorator,所以我不能说这是否会影响问题。但是我上面提到的事情会让你部分了解。

menubaroptions()
中没有
self
,因为你将它的第一个参数命名为
root
。请注意,在Tkinter变量上调用
.get()
可以保证为您提供的值不适合与小部件的
textvariable=
选项一起使用;传递var本身!很抱歉这么晚才回答,过去几天我都没能处理这个问题。你能详细说明一下如何传递Var本身吗?只要把trennzeichenentry=tkinter.Entry(…textvariable=root.entrystring,…).pack()修复了它。我很困惑,因为我可以发誓我以前试过,但显然没有。谢谢你指出这一点!