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

时间:2018-03-13 03:06:27

标签: python tkinter python-2.x

_Recommendation_by_food.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())

_MainScreen.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库来开发gui,因为你可以看到我已经给出了两个代码片段

  1. _MainScreen.py
  2. _Recommendation_by_food.py
  3. 当程序执行时,它会调用带有按钮的_MainScreen.py,所以如果用户选择一个选项,说"按食物推荐",它将打开另一个窗口并包含相关的输入文本框,一旦用户提供了价值并点击了提交按钮,该值就会显示在屏幕上,但这不会发生。

1 个答案:

答案 0 :(得分:0)

说实话,我不记得我为使你的代码工作所做的所有改变,我记得的主要是删除所有未使用和制作的模块和代码。它更紧密地跟随PEP 8 - Style Guide for Python Code(不完全)。

另一项重大修改是 initialize_screen派生您的InitializeScreen(重命名为tk.TK)课程,并改为调用tk.Toplevel()创建一个与主要窗口分开的第二个窗口。与此相关,我将调用tk.Tk()移到了主脚本中。

这两个脚本的版本我想一起展示了如何做你说你想要完成的事情:

<强> _Recommendation_by_food.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))

<强> _MainScreen.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()