在tkinter中而不是在控制台中定价输出 [英] Priting output in tkinter instead of console

查看:79
本文介绍了在tkinter中而不是在控制台中定价输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用graphlab来打印建议,并且我的tkinter GUI当前正在控制台中打印出建议结果,我迷失了如何在GUI中打印建议.

I'm using graphlab to print out recommendations and have my tkinter GUI which currently prints out the recommendation results in the console and I'm lost on how to get it to print in the GUI.

在"UserID"类中,有一个按钮可以调用"printrec"功能,但仅在控制台中打印出建议.

Class "UserID" is where i have a button that calls the "printrec" function but only prints out the recommendations in the console.

from Tkinter import *
import sqlite3




class Welcome():

     def __init__(self,master):


          self.master=master
          self.master.geometry('170x110+100+200')
          self.master.title('Welcome!')

          self.label1=Label(self.master,text='Music Recommender',fg='black', bg="red").grid(row=0,column=1)
          self.button1=Button(self.master,text="Enter UserID",fg='black', bg="blue", command=self.gotoUserID).grid(row=1,column=1)
          self.button2=Button(self.master,text="Comparing models",fg='black', bg="blue").grid(row=2,column=1)
          #self.button3=Button(self.master,text="Click to print recs",fg='red',command=self.rec).grid(row=3,column=1)
          self.button4=Button(self.master,text="Exit",fg='black' , bg="blue",command=self.exit).grid(row=4,column=1)




     def exit(self):
        #exit button#
          self.master.destroy()

     def gotoUserID(self):
        #USERID GUI#    
          root2=Toplevel(self.master)
          myGUI=UserID(root2)

class UserID():
    #userID class prints song recs 
     def __init__(self,master):


          self.userRec=int()

          self.master=master
          self.master.geometry('400x250+100+200')
          self.master.title('ID recommender')

          self.label2=Label(self.master,text='Welcome to the Music Recommender',fg='red').grid(row=0,column=0)
          self.label2=Label(self.master,text='Please enter UserID: ',fg='black').grid(row=3,column=0)

          self.ID=Entry(self.master,textvariable=self.userRec).grid(row=3,column=1)
          self.button4=Button(self.master,text="Get recommendations",fg='red',command=self.printrec).grid(row=7,column=0)
          self.button5=Button(self.master,text="Exit",fg='red',command=self.exit).grid(row=9,column=0)

     def printrec(self):
        #currently prints song recs in console
          print song_recommendations






def main():
    root=Tk()
    root.configure(background='blue')
    myGUIWelcome=Welcome(root)
    root.mainloop()

if __name__ == '__main__':
     main()

这是我用来推荐的模型.它使用graphlab程序包.

This is the model im using to recommend. It uses the graphlab package.

recs_model = gl.recommender.ranking_factorization_recommender.create(plays_df, user_id="userID", item_id="songID", target="plays")
recommendations = recs_model.recommend(users=["3a613180775197cd08c154abe4e3f67af238a632"])
song_recommendations = recommendations.join(songs_df, on="songID", how="inner").sort("rank")

这是我想出现在GUI中的输出,但是我不确定是否可以将这样的sframe打印到tkinter中.

This is the output I want to appear in my GUI but im not sure if its possible to print an sframe like this into tkinter.

+-------------------------------+--------------------+---------------+------+
|             userID            |       songID       |     score     | rank |
+-------------------------------+--------------------+---------------+------+
| 3a613180775197cd08c154abe4... | SOXTUWG12AB018A2E2 | 59.1517736392 |  1   |
| 3a613180775197cd08c154abe4... | SOYBLYP12A58A79D32 | 30.0328809695 |  2   |
| 3a613180775197cd08c154abe4... | SOFVLYV12A8C145D8F | 28.9258825259 |  3   |
| 3a613180775197cd08c154abe4... | SOVWBYM12A6D4F8A22 | 28.9166185336 |  4   |
| 3a613180775197cd08c154abe4... | SOAUWYT12A81C206F1 | 27.7749540286 |  5   |
| 3a613180775197cd08c154abe4... | SODCNEE12A6310E037 | 27.7024595218 |  6   |
| 3a613180775197cd08c154abe4... | SOAIWAB12A8C13710A | 24.2139894443 |  7   |
| 3a613180775197cd08c154abe4... | SOFFXAQ12A8AE45C2E | 23.7690011935 |  8   |
| 3a613180775197cd08c154abe4... | SONDKOF12A6D4F7D70 | 22.8345548587 |  9   |
| 3a613180775197cd08c154abe4... | SOJFQWU12A8C1448C7 | 22.2914831119 |  10  |
+-------------------------------+--------------------+---------------+------+
+--------------------------------+--------------------------------+
|             title              |            release             |
+--------------------------------+--------------------------------+
| Drop The Hammer (Album Ver...  |      A Search For Reason       |
|      Up And Up (Acoustic)      | Must Have Done Something Right |
|      Believe In Yourself       |           Questions            |
|  Video Killed The Radio Star   | Friends Reunited: Music Of...  |
|              Undo              |        Vespertine Live         |
| Sorrow (1997 Digital Remaster) | The Best Of David Bowie 19...  |
|           Blind Date           |         Anchors Aweigh         |
|         Death Chamber          |           Isolation            |
| Recado Falado (Metrô Da Sa...  |             2 Em 1             |
|      Rain On Your Parade       |      Rain On Your Parade       |
+--------------------------------+--------------------------------+
+------------------+------+
|    artistName    | year |
+------------------+------+
|     Kilgore      | 1998 |
|    Relient K     |  0   |
|       Us3        | 2004 |
|   The Buggles    | 1979 |
|      Björk       | 2001 |
|   David Bowie    |  0   |
|  Bouncing Souls  | 2003 |
| Fear My Thoughts | 2008 |
|  Alceu Valença   |  0   |
|      Duffy       |  0   |
+------------------+------+
[10 rows x 8 columns]

谢谢!

推荐答案

您可以制作gui:

from tkinter import *

root = Tk()

Console = Text(root)
Console.pack()

root.mainloop()

然后您可以重写打印功能:

and then you can rewrite the print function:

def write(*message, end = "\n", sep = " "):
    text = ""
    for item in message:
        text += "{}".format(item)
        text += sep
    text += end
    Console.insert(INSERT, text)

在这里,您可以在tkinter中获得输出,只需调用write()而不是print()

and here you have the output in tkinter, you just have to call write() instead of print()

这篇关于在tkinter中而不是在控制台中定价输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆