代码之家  ›  专栏  ›  技术社区  ›  Harshit

无法从gui tkinter文本框中获取值

  •  0
  • Harshit  · 技术社区  · 6 年前

    _推荐食物。py公司

    '''
    
    Created on Feb 24, 2018
    
    @author: Harshit Sinha
    '''
    import pandas as pd
    import os
    import Tkinter as tk
    from Tkinter import *
    import _tkinter
    from pygments.lexers._vim_builtins import command
    
    #from _MainScreen import B1
    
    win = tk.Tk()
    #win.update()
    win.title("RECOMMENDER SYSTEM FOR FOODS")
    win.geometry('500x500')
    
    
    #name = StringVar()
    '''
    id = tk.StringVar()
    Food1 = tk.StringVar()
    Food1_rating = tk.StringVar()
    '''
    
    class initialize_screen(tk.Tk):
    ##################user inputs
    
    
        ## User_name
        def __init__(self):
            tk.Tk.__init__(self)
        def _draw_screen(self):
            self.User_Name_Label=tk.Label(self,text="User Name :").grid(column=0, row=0)
    
            self.name = tk.StringVar()
            Entry(self,width=12,textvariable=self.name).grid(column=1, row=0)
    
    
            ## User_id
            self.User_id_Label=tk.Label(self, text="User id :").grid(column=0, row=1)
    
    
            self.id = tk.StringVar()
            self.User_id_Input=tk.Entry(self,width=12,textvariable=self.id).grid(column=1, row=1)
    
            ## Food1
            self.Food1_Label=tk.Label(self, text="Food1 :").grid(column=0, row=2)
    
    
            self.Food1 = tk.StringVar()
            self.Food1_Input=tk.Entry(self,width=12,textvariable=self.Food1).grid(column=1, row=2)
    
            ## Food1_rating
            self.Food1_rating_Label=tk.Label(self, text="Rating :").grid(column=2, row=2)
    
    
            self.Food1_rating = tk.StringVar()
            self.Food1_rating_Input=tk.Entry(self,width=12,textvariable=self.Food1_rating).grid(column=3, row=2)
    
            ## Food2
            self.Food2_Label=tk.Label(self, text="Food2 :").grid(column=0, row=3)
    
    
            Food2 = tk.StringVar()
            self.Food2_Input=tk.Entry(self,width=12,textvariable=Food2).grid(column=1, row=3)
    
             ## Food2_rating
            self.Food2_rating_Label=tk.Label(self, text="Rating :").grid(column=2, row=3)
    
    
            self.Food2_rating = tk.StringVar()
            self.Food2_rating_Input=tk.Entry(self,width=12,textvariable=self.Food2_rating).grid(column=3, row=3)
    
    
            ## Food3
            self.Food3_Label=tk.Label(self,text="Food3 :").grid(column=0, row=4)
    
    
            self.Food3 = tk.StringVar()
            self.Food3_Input=tk.Entry(self,width=12,textvariable=self.Food3).grid(column=1, row=4)
    
    
             ## Food3_rating
            self.Food3_rating_Label=tk.Label(self, text="Rating :").grid(column=2, row=4)
    
    
            self.Food3_rating = tk.StringVar()
            self.Food3_rating_Input=tk.Entry(self,width=12,textvariable=self.Food3_rating).grid(column=3, row=4)
    
    
            ##submit button
            self.submit = Button(self, text="Submit",command=self.print_val).grid(column=0,row=5)
    
            ##reset button
            self.reset = tk.Button(self, text="Reset").grid(column=1, row=5)
    
    #        win.resizable(0, 0)
    #        self.User_Name_Input.focus()
    #        win.mainloop()
            print(self.name.get())
    
    
        def print_val(self):
    #        global name,id,Food1,Food1_rating
    
            print('hi')
            name1 = self.name.get()
            print(str(name1))
    #        print(self.Food1.get())
    #        print(self.Food1_rating.get())
    #        print(self.Food2_Input.get())
    #        print(self.Food2_rating_Input.get())
    #        print(self.Food3_Input.get())
    #        print(self.Food3_rating_Input.get())
    

    _主屏幕。py公司

    '''
    Created on Feb 24, 2018
    
    @author: Harshit Sinha
    '''
    import pandas as pd
    import os
    import Tkinter as tk
    import _tkinter
    
    
    #bring up MainScreen
    import _Recommendation_by_food as fd
    from _Recommendation_by_food import *
    
    
    def call_screen_1():
        fd=initialize_screen()
        fd._draw_screen()
    #    fd.__init__()
    
    LARGE_FONT= ("Verdana", 12)
    
    B1 = tk.Button(win,text ="Recommendation By Food",width=30, command=call_screen_1)
    #B2 = tk.Button(win,text ="Recommendation By Ingredients",width=30,command= call_screen_2)
    #B3 = tk.Button(win,text ="Recommendation By Nutrition",width=30,command= call_screen_3)
    
    B1.pack()
    #B2.pack()
    #B3.pack()
    
    #win.resizable(0, 0)
    
    win.mainloop()
    

    我正在用python做一个关于机器学习的POC,但遇到了一个问题。 我正在使用 tkinter 如您所见,我在上面给出了两段代码片段

    1. _MainScreen.py
    2. _Recommendation_by_food.py

    程序执行时调用 _主屏幕。py公司 它有按钮,所以如果用户选择一个选项,比如“食物推荐”,它将打开另一个窗口并包含相关文本框供输入,一旦用户提供值并单击提交按钮,该值应显示在屏幕上,但这不会发生。

    1 回复  |  直到 6 年前
        1
  •  0
  •   martineau Nae    6 年前

    老实说,我不记得我为使代码正常工作所做的所有更改,我记得的主要更改是删除所有未使用的模块和代码,并使其遵循 PEP 8 - Style Guide for Python Code 更接近(不完全)。

    另一个主要修改是 导出您的 initialize_screen (已重命名 InitializeScreen )分类自 tk.TK ,并让它改为呼叫 tk.Toplevel() 创建与主窗口分离的第二个窗口。与此相关,我将呼叫转移到 tk.Tk() 进入主脚本。

    以下是两个脚本的版本,我认为这两个脚本共同展示了如何完成您所说的尝试:

    _推荐食物。py公司

    import Tkinter as tk
    
    class InitializeScreen(object):
        def __init__(self):
            self.win = tk.Toplevel(takefocus=True)  # Create a new window.
    
        def draw_screen(self):
            ## User_Name
            self.User_Name_Label = tk.Label(self.win, text="User Name :")
            self.User_Name_Label.grid(column=0, row=0)
            self.User_Name = tk.StringVar()
            self.Name_Input = tk.Entry(self.win, width=12, textvariable=self.User_Name)
            self.Name_Input.grid(column=1, row=0)
    
            ## User_id
            self.User_id_Label = tk.Label(self.win, text="User id :")
            self.User_id_Label.grid(column=0, row=1)
            self.id = tk.StringVar()
            self.User_id_Input = tk.Entry(self.win, width=12, textvariable=self.id)
            self.User_id_Input.grid(column=1, row=1)
    
            ## Food1
            self.Food1_Label = tk.Label(self.win, text="Food1 :")
            self.Food1_Label.grid(column=0, row=2)
            self.Food1 = tk.StringVar()
            self.Food1_Input = tk.Entry(self.win, width=12, textvariable=self.Food1)
            self.Food1_Input.grid(column=1, row=2)
    
            ## Food1_rating
            self.Food1_rating_Label = tk.Label(self.win, text="Rating :")
            self.Food1_rating_Label.grid(column=2, row=2)
            self.Food1_rating = tk.StringVar()
            self.Food1_rating_Input = tk.Entry(self.win, width=12, textvariable=self.Food1_rating)
            self.Food1_rating_Input.grid(column=3, row=2)
    
            ## Food2
            self.Food2_Label = tk.Label(self.win, text="Food2 :")
            self.Food2_Label.grid(column=0, row=3)
            Food2 = tk.StringVar()
            self.Food2_Input = tk.Entry(self.win, width=12, textvariable=Food2)
            self.Food2_Input.grid(column=1, row=3)
    
            ## Food2_rating
            self.Food2_rating_Label = tk.Label(self.win, text="Rating :")
            self.Food2_rating_Label.grid(column=2, row=3)
            self.Food2_rating = tk.StringVar()
            self.Food2_rating_Input = tk.Entry(self.win, width=12, textvariable=self.Food2_rating)
            self.Food2_rating_Input.grid(column=3, row=3)
    
            ## Food3
            self.Food3_Label = tk.Label(self.win, text="Food3 :")
            self.Food3_Label.grid(column=0, row=4)
            self.Food3 = tk.StringVar()
            self.Food3_Input = tk.Entry(self.win, width=12, textvariable=self.Food3)
            self.Food3_Input.grid(column=1, row=4)
    
            ## Food3_rating
            self.Food3_rating_Label = tk.Label(self.win, text="Rating :")
            self.Food3_rating_Label.grid(column=2, row=4)
            self.Food3_rating = tk.StringVar()
            self.Food3_rating_Input = tk.Entry(self.win, width=12, textvariable=self.Food3_rating)
            self.Food3_rating_Input.grid(column=3, row=4)
    
            ## Submit button
            self.submit = tk.Button(self.win, text="Submit", command=self.print_val)
            self.submit.grid(column=0,row=5)
    
            ## Reset button
            self.reset = tk.Button(self.win, text="Reset")
            self.reset.grid(column=1, row=5)
    
    
        def print_val(self):
            name1 = self.User_Name.get()
            print('name1: {!r}'.format(name1))
    

    _主屏幕。py公司

    import Tkinter as tk
    from _Recommendation_by_food import InitializeScreen
    
    def call_screen_1():
        fd = InitializeScreen()
        fd.draw_screen()
    
    win = tk.Tk()
    win.title('Recommender System For Foods')
    win.geometry('500x500')
    
    B1 = tk.Button(win, text="Recommendation By Food", width=30, command=call_screen_1)
    B1.pack()
    
    win.mainloop()