Python -打印到GUI而不是终端

c3frrgcw  于 2022-12-24  发布在  Python
关注(0)|答案(1)|浏览(147)

非常新的CustomTkinter,我正在尝试创建一个GUI应用程序,返回一个预测您写的tweet。输出发生在终端,我希望它打印在GUI而不是终端,如果有人能帮助我,这将是非常感谢!所有我正在尝试做的是打印我的结果在GUI上,我知道打印直接到终端,但我不知道如何解决这个问题...
这是我的密码

import customtkinter
import tkinter
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from scipy.special import softmax

app = customtkinter.CTk()
app.geometry("700x500")
app.title("Twitter Sentiment Analyser")

frame_1 = customtkinter.CTkFrame(master=app)
frame_1.pack(pady=20, padx=60, fill="both", expand=True)

label_1 = customtkinter.CTkLabel(master=frame_1, justify=tkinter.LEFT, text="Twitter Sentiment Analyses System")
# label_1.pack(pady=12, padx=10)
# label_1.pack(side="left", pady=12)
label_1.place(x=20, y=20)

tweet = input()
tweet1 = customtkinter.CTkTextbox(master= frame_1, height= 100, width=380)
tweet1.pack(pady=12, padx=10)
tweet1.place(y= 150, x=100)
tweet1 = tweet

# def predict():
    # p = entry_1.get("1.0",'end-1c')
    # label_2 = customtkinter.CTkLabel(master=frame_1, text="The Tweet is: ")
tweet_words = []

for word in tweet.split(' '):
    if word.startswith('@') and len(word) > 1:
        word = '@user'
    
    elif word.startswith('http'):
        word = "http"
    tweet_words.append(word)

tweet_proc = " ".join(tweet_words)

# load model and tokenizer
roberta = "cardiffnlp/twitter-roberta-base-sentiment"

model = AutoModelForSequenceClassification.from_pretrained(roberta)
tokenizer = AutoTokenizer.from_pretrained(roberta)

labels = ['Negative', 'Neutral', 'Positive']

# sentiment analysis
encoded_tweet = tokenizer(tweet_proc, return_tensors='pt')
# output = model(encoded_tweet['input_ids'], encoded_tweet['attention_mask'])
output = model(**encoded_tweet)

scores = output[0][0].detach().numpy()
scores = softmax(scores)
def pre():
    for i in range(len(scores)):
    
        l = labels[i]
        s = scores[i]
        print(l,s)

buutton_1 = customtkinter.CTkButton(master=frame_1, command=pre, text="Predict")
buutton_1.pack(pady=12, padx=10)
buutton_1.place(y=290, x=90)
disho6za

disho6za1#

这将有助于理解tkinter

import tkinter

#create root 
app = tkinter.Tk()
app.geometry('1280x720')
#create textbox for get input in gui  
input_text_box = tkinter.Text(app ,width=700 ,height=300 ,fg='#ffffff' ,bg="#101010")
input_text_box.place(x=0)

#create textbox for get output in gui
output_text_box = tkinter.Text(app ,width=700 ,height=200 ,fg='#ffffff' ,bg="#101010")
output_text_box.place(y=350 ,x=0)

def prediction_tweet(input_text):
    predicted_tweet = input_text
    
    # you can add your process for get prediction text 
    return predicted_tweet

input_text = ""
def display():
    global input_text
    
    #read values from textbox 
    input_text_new  = input_text_box.get("0.0","end")

    if input_text != input_text_new :
        input_text = input_text_new 
        
        #get predicted tweet
        predicted_tweet = prediction_tweet(input_text)
        
        #delete all text in output textbox # need to reset textbox before display
        output_text_box.delete('0.0','end')

        #display predicted tweet 
        output_text_box.insert('end',predicted_tweet)
    #loop
    app.after(100,display)
     #         ^
     #     100 milisecond delay on loop 
#call def display
display()

app.mainloop()

相关问题