Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.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 使用Tkinter继承输入框小部件的问题_Python_Tkinter - Fatal编程技术网

Python 使用Tkinter继承输入框小部件的问题

Python 使用Tkinter继承输入框小部件的问题,python,tkinter,Python,Tkinter,在Tkinter中使用Python 2.11编写GUI,使用OOP方法并尝试学习继承。在下面的GUI中,我有两个条目小部件。标签为“Enter Value-Not Return”的第一个小部件使用从Tkinter Entry类继承的类EntryField。在此输入框中输入值时,将调用方法setDesVol,但不会返回self.desV中的值。如果我尝试另一条语句self.desV=self.parent.get()(已注释掉),则会给出一个错误,表明MainWindow实例没有属性get 现在,

在Tkinter中使用Python 2.11编写GUI,使用OOP方法并尝试学习继承。在下面的GUI中,我有两个条目小部件。标签为“Enter Value-Not Return”的第一个小部件使用从Tkinter Entry类继承的类EntryField。在此输入框中输入值时,将调用方法setDesVol,但不会返回self.desV中的值。如果我尝试另一条语句self.desV=self.parent.get()(已注释掉),则会给出一个错误,表明MainWindow实例没有属性get

现在,当我调用分配给对象entValue的下一个条目小部件时,它工作得非常好,因为它包含在MainWindow类中,并使用方法setValue和self.entValue.get()按预期工作

有没有人知道我在EntryField类中从输入框中获取值时做错了什么。我的计划是在最终的GUI中有更多的输入框,并希望每个输入框都有自己的关联方法,如setDesVol,以保持代码更干净、更易于管理。我已经花了大约3天的时间试图弄明白这一点,所以任何帮助都是感激的。代码附在下面。谢谢

import Tkinter as tk
import ttk
import tkMessageBox as mBox

class MainWindow(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

        self.olFrame = tk.LabelFrame(text = 'Open Loop Frame', bg = 'red')
        self.olFrame.grid(column = 0, row = 0, sticky = 'w')
        self.voltLabel = tk.Label(self.olFrame,
                text = 'Enter Value - Does Not Return ',
                font = ("Times", 10), bg = 'red').grid(column = 0, row = 1)

        #entry box code that does not return anything
        self.entDesVolt = EntryField(self.olFrame,bg = 'red')
        self.entDesVolt.bind("<Return>", EntryField(self).setDesVol)
        self.entDesVolt.grid(column = 1, row = 1)

        #this entry box code works fine
        self.mezLabel = tk.Label(self.olFrame,
                text = 'Enter Another Value - works right ',
                font = ("Times", 10), bg = 'grey').grid(column = 0, row = 2)
        self.entValue = tk.Entry(self.olFrame, bg = 'grey')
        self.entValue.bind("<Return>", self.setValue)
        self.entValue.grid(column = 1, row = 2)

    def setValue(self, event):
        print self.entValue.get()
        print 10 + float(self.entValue.get())

class EntryField(tk.Entry):
    def __init__(self, parent, *args, **kwargs):
        tk.Entry.__init__(self, parent, *args, **kwargs)
        self.parent = parent
        print "self.parent in EntryField is type:  ", type(self.parent)

    def setDesVol(self,event):
        self.desV = EntryField(self).get()  #doesnt return anything in print statement
        #self.desV = self.parent.get()  #MainWindow instance has no attribute get
        #self.desV = tk.Entry(self).get()  #doesnt return anything in print statement
        print "self des V is:  ", self.desV

root = tk.Tk()
root.geometry('500x400')
root.title('Test GUI')
root.configure(background = "black")

a = MainWindow(root)
root.mainloop()
将Tkinter作为tk导入
导入ttk
将tkMessageBox作为mBox导入
类主窗口(tk.Frame):
定义初始化(自、父、*args、**kwargs):
tk.Frame.\uuuuu init\uuuuuu(self,parent,*args,**kwargs)
self.parent=parent
self.olFrame=tk.LabelFrame(text='Open-Loop-Frame',bg='red')
self.olFrame.grid(列=0,行=0,粘性='w')
self.voltlab=tk.Label(self.olFrame,
text='输入值-不返回',
字体=(“Times”,10),背景=“red”).grid(列=0,行=1)
#不返回任何内容的输入框代码
self.entDesVolt=EntryField(self.olFrame,bg='red')
self.entDesVolt.bind(“,EntryField(self.setDesVol))
self.entDesVolt.grid(列=1,行=1)
#此输入框代码工作正常
self.mezLabel=tk.Label(self.olFrame,
text='输入另一个值-正确工作',
字体=(“Times”,10),背景=“grey”).grid(列=0,行=2)
self.entValue=tk.Entry(self.olFrame,bg='grey')
self.entValue.bind(“,self.setValue)
self.entValue.grid(列=1,行=2)
def设置值(自身、事件):
打印self.entValue.get()
打印10+浮点(self.entValue.get())
类入口字段(tk.Entry):
定义初始化(自、父、*args、**kwargs):
tk.Entry.\uuuuu init\uuuuuuuuu(self,parent,*args,**kwargs)
self.parent=parent
打印“输入字段中的self.parent类型:”,类型(self.parent)
def setDesVol(自身,事件):
self.desV=EntryField(self.get()#在print语句中不返回任何内容
#self.desV=self.parent.get()#主窗口实例没有属性get
#self.desV=tk.Entry(self.get()#在print语句中不返回任何内容
打印“self des V is:”,self.desV
root=tk.tk()
根几何体('500x400')
root.title('测试GUI')
root.configure(background=“black”)
a=主窗口(根)
root.mainloop()

EntryField
类的
setDesVol
方法中,您可以使用:

`self.desV = EntryField(self).get()` 
因此,创建一个新的
EntryField
实例并调用其
get
方法,该方法返回
'
,因为默认情况下条目为空。要获取当前
EntryField
的内容,只需调用它从`tk.Entry'继承的
get
方法:

`self.desV = self.get()`
执行绑定时,
main窗口
类中也存在类似问题:

self.entDesVolt = EntryField(self.olFrame, bg='red')
self.entDesVolt.bind("<Return>", EntryField(self).setDesVol)
self.entDesVolt = EntryField(self.olFrame, bg='red')
self.entDesVolt.bind("<Return>", self.entDesVolt.setDesVol)