简体   繁体   English

python传递变量tkinter

[英]python pass variable tkinter

I'm new im Python, just started to learn about class and tkinter, so forgive me "messy" code. 我是Python的新手,刚刚开始学习类和tkinter,所以请原谅我“凌乱”的代码。 I'm trying to enter some string to field nr1, and after click a button, print this string in console and store this value for later: 我尝试在字段nr1中输入一些字符串,然后单击按钮后,在控制台中打印此字符串并将其存储为以后使用:

from tkinter import Tk, BOTH, RIGHT, RAISED, BOTTOM, TOP, X, StringVar
from tkinter.ttk import Frame, Button, Entry


class AD(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent, v=None, raw_input=None)
        self.parent = parent
        self.parent.geometry("250x150+300+300")
        self.parent.title("Trolollo")
        self.parent.resizable(False, False)
        self.inp = None
        self.v = StringVar()
        self.raw_input = None

        self.initUI()

    def user_input(self):
        global inp
        a = self.raw_input(self.v.get())
        inp = a
        return inp


    def initUI(self):
        self.pack(fill=BOTH, expand=True)

        frame = Frame(self, relief=RAISED, borderwidth=0)
        frame.pack(fill=BOTH, expand=True)

        self.entry1 = Entry(frame, textvariable=self.v)
        self.entry1.pack(side=TOP, fill=X, expand=False, padx=2, pady=2)
        self.entry1.focus_set()

        rename_button = Button(frame, text="Dispaly text", command =         self.user_input())
        rename_button.pack(side=TOP, expand=False, padx=2, pady=2)

        entry2 = Entry(frame)
        entry2.pack(side=TOP, fill=X, expand=False, padx=2, pady=2)


        quit_button = Button(self, text="Quit", command=self.quit)
        quit_button.pack(side=RIGHT, padx=5, pady=5)

        ok_button = Button(self, text="OK")
        ok_button.pack(side=RIGHT, padx=5, pady=5)


def main():
    root = Tk()


    app = AD(root)
    root.mainloop()


if __name__ == '__main__':
    main()

After executing code, i get: TypeError: 'NoneType' object is not callable 执行代码后,我得到:TypeError:'NoneType'对象不可调用

Any help would me appreciated 任何帮助,我将不胜感激

ISSUES: 问题:

  1. First issue laid in your rename_button's option " command=self.user_input() ". 第一个问题放在您的named_button的选项“ command = self.user_input() ”中。 You were suppose to name the function and not execute the function. 您应该命名该函数而不执行该函数。 Putting the () symbol meant you executed the function when your code loaded, ie it executed once w/o pressing the rename button . 放置()符号意味着您在代码加载时执行了该函数,即,在不按重命名按钮的情况下执行了一次。
  2. Second issue was the erroneous code in your function user_input . 第二个问题是函数user_input中的错误代码。 This caused your error msg. 这导致您的错误消息。

ANSWER: Code with the suggested corrections. 解答:编码建议的更正。

from tkinter import *
from tkinter.ttk import *


class AD(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent, v=None, raw_input=None)
        self.parent = parent
        self.parent.geometry("250x150+300+300")
        self.parent.title("Trolollo")
        self.parent.resizable(False, False)
        self.inp = None
        self.v = StringVar()
        self.raw_input = None

        self.initUI()

    def user_input(self):
        # Get entry1 value, store it as an attribute and print to console
        self.raw_input = self.v.get()
        print(self.raw_input)


    def initUI(self):
        self.frame = Frame(self, relief=RAISED, borderwidth=0)
        self.frame.pack(fill=BOTH, expand=True)

        self.entry1 = Entry(self.frame, textvariable=self.v)
        self.entry1.pack(side=TOP, fill=X, expand=False, padx=2, pady=2)
        self.entry1.focus_set()


        #self.rename_button = Button(self.frame, text="Dispaly text",
        #                            command = self.user_input())
        self.rename_button = Button(self.frame, text="Display text",
                                    command = self.user_input)
        self.rename_button.pack(side=TOP, expand=False, padx=2, pady=2)


        # You can remove the triple quotes to display these widgets 
        """
        self.entry2 = Entry(self.frame)
        self.entry2.pack(side=TOP, fill=X, expand=False, padx=2, pady=2)


        self.quit_button = Button(self.frame, text="Quit", command=self.quit)
        self.quit_button.pack(side=RIGHT, padx=5, pady=5)

        self.ok_button = Button(self.frame, text="OK")
        self.ok_button.pack(side=RIGHT, padx=5, pady=5)

        """

        self.pack(fill=BOTH, expand=True)


def main():
    root = Tk()


    app = AD(root)
    root.mainloop()

Your GUI : 您的GUI: 在此处输入图片说明

SUGGESTIONS: 建议:

  • Do remember to put self. 切记放自我。 in front of your widgets. 在您的小部件前面。
  • Do test one widget at a time to help you debug your code. 一次测试一个小部件,以帮助您调试代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM