StringVar()。get()没有返回字符串

时间:2017-04-18 03:09:19

标签: python tkinter

我正在尝试输入用户想要的页码。他们应该键入一个数字,然后单击“提交”按钮。为了测试它,我只想打印他们输入的内容,然后关闭窗口。我一直在关注:http://effbot.org/tkinterbook/entry.htm作为向导,但我很难过。

为什么

print(temp) 

不打印出数字到控制台?

现在打印出来:

<bound method IntVar.get of <tkinter.IntVar object at 0x000001FBC85353C8>>          

我已经清理了一点代码:

import sys
from file import *
from page import *
from view import View
import tkinter as tk
from tkinter import *


class ViewGui:
def __init__(self):

    #Included in the class, but unrelated to the question:
    self._file = File("yankee.txt", 25)
    self.pages = self._file.paginate()
    self.initial_list = self.pages[0].readpage(self._file.fo)
    self.initial_string = ''.join(self.initial_list)


    # Create root
    self.root = Tk()
    self.root.wm_title("yankee.txt - page 1")
    # Create frame for buttons
    self.bframe = Frame(self.root)
    self.bframe.pack(side=BOTTOM, fill=X)
    self.tbutton = tk.Button(self.bframe, text="Top", command=lambda a="top": self.clicks(a)).pack(side=LEFT, expand=1, fill=X)
    self.bbutton = tk.Button(self.bframe, text="Bottom", command=lambda a="bottom": self.clicks(a)).pack(side=LEFT, expand=1, fill=X)
    self.ubutton = tk.Button(self.bframe, text="Up", command=lambda a="up": self.clicks(a)).pack(side=LEFT, expand=1, fill=X)
    self.dbutton = tk.Button(self.bframe, text="Down", command=lambda a="down": self.clicks(a)).pack(side=LEFT, expand=1, fill=X)
    self.pbutton = tk.Button(self.bframe, text="Page", command=lambda a="page": self.pageclicks()).pack(side=LEFT, expand=1, fill=X)
    self.qbutton = tk.Button(self.bframe, text="Quit", command=quit).pack(side=LEFT, expand=1, fill=X)
    # Create and pack Text
    self.T = Text(self.root, height=35, width=60, wrap=NONE)
    self.T.pack(side=TOP, fill=X)
    # Create and pack Scrollbar
    self.S = Scrollbar(self.root, orient=HORIZONTAL, command=self.T.xview)
    self.S.pack(side=BOTTOM, fill=X)
    # Attach Text to Scrollbar
    self.T.insert('1.0', self.initial_string)
    self.T.config(xscrollcommand=self.S.set, state=DISABLED)
    self.S.config(command=self.T.xview)

def pageclicks(self):
    print("pageClicks is getting called at least...")
    pop = Tk()
    pop.wm_title("Page Number")
    pop.label = Label(pop, text="Enter a Page Number:", width=35)
    pop.label.pack(side=TOP)
    pop.entrytext = IntVar()
    Entry(pop, textvariable=pop.entrytext).pack()
    pop.submitbuttontext = StringVar()
    Button(pop, text="Submit", command=lambda a=pop: self.submitted(a)).pack(side=LEFT, pady=5, padx=40)
    pop.cancelbuttontext = StringVar()
    Button(pop, text="Cancel", command=pop.destroy).pack(side=LEFT, pady=5, padx=40)

def submitted(self, a):
    print('submitted is getting called')
    temp = (a.entrytext.get)
    print(temp)

def clicks(self, a):
    print("you clicked clicks with the " + a)
    self.root.wm_title(self._file.filename + " - Page " + self._file.buttonHandler(a))

if __name__ == "__main__":
    vg = ViewGui()
    vg.root.mainloop()

2 个答案:

答案 0 :(得分:2)

创建新窗口时不应使用Tk(),必须使用tk.Toplevel()

必须改变:

pop = Tk()

pop = tk.Toplevel()

您还应该使用get(),而不仅仅是get。必须改变:

temp = (a.entrytext.get)

temp = a.entrytext.get()

<强>代码:

def pageclicks(self):
        print("pageClicks is getting called at least...")
        pop = tk.Toplevel()
        pop.wm_title("Page Number")
        pop.label = Label(pop, text="Enter a Page Number:", width=35)
        pop.label.pack(side=TOP)
        pop.entrytext = IntVar()
        Entry(pop, textvariable=pop.entrytext).pack()
        pop.submitbuttontext = StringVar()
        Button(pop, text="Submit", command=lambda a=pop: self.submitted(a)).pack(side=LEFT, pady=5, padx=40)
        pop.cancelbuttontext = StringVar()
        Button(pop, text="Cancel", command=pop.destroy).pack(side=LEFT, pady=5, padx=40)

def submitted(self, a):
    print('submitted is getting called')
    temp = a.entrytext.get()
    print(temp)

答案 1 :(得分:0)

对这两种方法进行了更改,现在它正在运行。

def pageclicks(self):
    print("pageClicks is getting called at least...")
    pop = Tk()
    pop.wm_title("Page Number")
    pop.label = Label(pop, text="Enter a Page Number:", width=35)
    pop.label.pack(side=TOP)
    pop._entry = Entry(pop)
    pop._entry.pack()
    pop._entry.focus_set()
    Button(pop, text="Submit", command=lambda a=pop: self.submitted(a)).pack(side=LEFT, pady=5, padx=40)
    Button(pop, text="Cancel", command=pop.destroy).pack(side=LEFT, pady=5, padx=40)

def submitted(self, _pop):
    temp = _pop._entry.get()
    print(temp)
    _pop.destroy()
相关问题