4

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.

Shirohige
  • 243
  • 1
  • 3
  • 14
  • As I am new to tkinter haven't tried much. – Shirohige Jun 12 '17 at 07:14
  • Possible duplicate of [Does tkinter have a table widget?](https://stackoverflow.com/questions/9348264/does-tkinter-have-a-table-widget) – SiHa Jun 12 '17 at 07:28
  • 4
    stackoverflow isn't a free code-writing service. You need to attempt to solve the problem yourself, and then as a _specific_ question when you get to the part of the problem that you are struggling with. – Bryan Oakley Jun 12 '17 at 11:54
  • @BryanOakley Actually, I am stuck after getting the text file data and wanted to know about how can I create a column in front of that particular sentence that I am getting from the textfile? – Shirohige Jun 12 '17 at 14:10
  • @Shirohige what are you doing with this column you want to create? – Mike - SMT Jun 14 '17 at 12:52
  • @SierraMountainTech I want to create 2 columns in 1st column I want to get the sentences from the text file and in the 2nd column, I want to manually select from the scrollbar whether the sentence is positive, negative or neutral. I have gone through the Tkinter tutorials still I didn't understand how to code it. – Shirohige Jun 14 '17 at 13:16
  • your use of the word column is confusing. You want to create 2 columns in the 1st column? That doesn't make any sense. Maybe you are trying to create 2 columns that are both left of the rest of the program? – Mike - SMT Jun 14 '17 at 13:19
  • @SierraMountainTech I've uploaded an image of my table format. 1st column contains sentences from a text file. And 2nd column contains a scrollbar to assign sentiment for a particular sentence. – Shirohige Jun 14 '17 at 13:51

1 Answers1

7

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()
Josselin
  • 2,593
  • 2
  • 22
  • 35