从Entry小部件tkinter存储数据

问题描述 投票:0回答:1

我正在尝试从Entry小部件中获取String并将其存储到变量中。 我在tkinter中使用菜单项来执行此操作,我尝试了许多不同的操作,但仍然无法正常工作。 这是我的第一个python程序。

 from tkinter import *

 class application(Frame):
   """ A Gui application with three buttons"""

     def __init__(self, master = None):
    """ Inititalization the fram"""
        Frame.__init__(self,master)
        self.master = master
        #self.grid()
        self.create_widgets()



     def create_widgets(self): 

        self.master.title("My GUI")
        self.pack(fill=BOTH, expand=1)
        buttons1 = Button(self,text = "this wroks", command= self.program_button)
        buttons1.place(x=250,y=250)
        #self.buttons1.place(x=5, y=10)

        menu = Menu(self.master)
        self.master.config(menu=menu)

        test = StringVar()
        eBox = Entry(self, textvariable=test)
        #eBox.pack()
        eBox.grid()



        file = Menu(menu)
        file.add_command(label='Save', command=self.text_capture)
        file.add_command(label='Exit', command=self.program_exit)
        menu.add_cascade(label='File', menu=file)



    def program_button(self):
         exit()

    def program_exit(self):
        exit()
    def text_capture(test):
        #test = StringVar()
        mtext = test.get()
        print(test) # this prints py_Var1 ???
        print(mtext) 
        print(test.get())

root = Tk()

root.geometry("500x500")

app = application(root)


root.mainloop()
tkinter python-3.5 tkinter-entry
1个回答
0
投票

我已经更改了一些代码,但总的来说,您已经非常接近所需的最终代码。 我更改的位如下:

test = StringVar()
eBox = Entry(self, textvariable=test)

self.test = StringVar()
eBox = Entry(self, textvariable=self.test)

def text_capture(test):
        mtext = test.get()
        print(test)
        print(mtext) 
        print(test.get())

def text_capture(self):
        mtext = self.test.get()
        print(self.test)
        print(mtext) 
        print(self.test.get())

完整的工作代码如下:

from tkinter import *

class application(Frame):
    """ A Gui application with three buttons"""
    def __init__(self, master = None):
        """ Inititalization the fram"""
        Frame.__init__(self,master)
        self.master = master
        #self.grid()
        self.create_widgets()



    def create_widgets(self):
        self.master.title("My GUI")
        self.pack(fill=BOTH, expand=1)
        buttons1 = Button(self,text = "this wroks", command= self.program_button)
        buttons1.place(x=250,y=250)
        #self.buttons1.place(x=5, y=10)

        menu = Menu(self.master)
        self.master.config(menu=menu)

        self.test = StringVar()
        eBox = Entry(self, textvariable=self.test)
        #eBox.pack()
        eBox.grid()

        file = Menu(menu)
        file.add_command(label='Save', command=self.text_capture)
        file.add_command(label='Exit', command=self.program_exit)
        menu.add_cascade(label='File', menu=file)



    def program_button(self):
        exit()

    def program_exit(self):
        exit()

    def text_capture(self):
        mtext = self.test.get()
        print(self.test)
        print(mtext) 
        print(self.test.get())

root = Tk()

root.geometry("500x500")

app = application(root)


root.mainloop()
© www.soinside.com 2019 - 2024. All rights reserved.