Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a column from desktop text file and identifying it as positive, negative or neutral sentence in python tkinter

I want to create a (nx2) table in python gui, where n is my no.of sentences in a text file and my second column will consist of a scrollbar where I can manually assign (positive or negative or neutral) value to it.Currently, I am able to get the sentences from text file using the below code:-

root=Tk()

def openInstruktion():
    from subprocess import call
    call("notepad c:\\Users\\Desktop\\tweets.txt")

instruktionBtn = Button(root, text='tweets', command=openInstruktion)
instruktionBtn.grid(row=6, column=0)
root.mainloop()

Gui should be like this:-Image

Thanks for your help in advance.

like image 741
Shirohige Avatar asked Mar 09 '23 21:03

Shirohige


1 Answers

Screenshot

First of all, to read the tweets from your text file try using .readlines():

# Get the list of tweets from a text file
with open("c:\\Users\\summert\\Desktop\\tweets.txt") as f:
    tweets = [x.strip() for x in f.readlines()]

Then, a simple way to create your "table" is to align widgets using the grid method.

Finally, you can add a scrollbar if the table gets too long:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
label_tweet = tk.Label(root, text="Tweet", width=28)
label_tweet.grid(row=0, column=0, padx=(20,0), pady=5)
label_sentiment = tk.Label(root, text="Sentiment", width=15)
label_sentiment.grid(row=0, column=1, padx=(0,30), pady=5)

# Create a table container with a scrollbar using a canvas
table_container = tk.Frame(root)
table_container.grid(row=1, column=0, columnspan=2, padx=20)
canvas = tk.Canvas(table_container, highlightthickness=0)
frame = tk.Frame(canvas)
vsb = ttk.Scrollbar(table_container, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=vsb.set)
canvas.pack(side="left", fill="both", expand=True)
vsb.pack(side="left", fill="y")
canvas.create_window((0,0), window=frame, anchor="nw")

# Put the tweets in a hand-made "table"
possibleSentiments = ["Positive", "Neutral", "Negative"]
sentimentBoxes = []
for i, tweet in enumerate(tweets):
    # Create an entry box for the tweet
    e = ttk.Entry(frame, width=35)
    e.insert(0, tweet)
    e.grid(row=i+1, column=0)
    # Create a combobox for the associated sentiment
    c = ttk.Combobox(frame, values=possibleSentiments, width=12, state="readonly")
    c.set(possibleSentiments[1])
    c.grid(row=i+1, column=1)
    sentimentBoxes.append(c)

# Add a button to save the tweets and sentiments in a CSV file
def saveSentiments():
    sentiments = [c.get() for c in sentimentBoxes]
    print(sentiments)
    with open('tweetSentiments.csv', 'w') as csvfile:
        for i in range(len(tweets)):
            csvfile.write('"{}","{}"\n'.format(tweets[i], sentiments[i]))

button = tk.Button(root, text="Save sentiments", command=saveSentiments)
button.grid(row=2, column=0, columnspan=2, pady=10)

# Resize the canvas
root.update()
frame_width = frame.winfo_width()
canvas.config(width=frame_width, height=140)
canvas.configure(scrollregion=canvas.bbox("all"))

# Launch the app
root.mainloop()
like image 158
Josselin Avatar answered Apr 06 '23 17:04

Josselin