无法检索CheckBox Value Tkinter

时间:2017-07-17 13:28:58

标签: python-3.x tkinter tkinter-canvas

在底部def中,我试图检索“1”或“0”以显示是否选中了复选框。当我运行代码时,我只收到“0”,无论检查什么,什么不检查。这背后的原因是什么?我是Python的新手,很抱歉,如果非常明显的话。

import tkinter as tk
import nltk
import string
import pandas as pd
import csv
from tkinter import *
from tkinter import ttk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from collections import Counter
from tabulate import tabulate

start = input('Would you like to start by selecting a document? (yes or no): ')
if start == 'yes':

    #Document selection
    root = tk.Tk()
    root.withdraw()
    file_path = filedialog.askopenfilename()

    dataFile = open(file_path).read()
    lines = dataFile.lower()
    tokens = nltk.word_tokenize(lines)


#Closes program if the program is not needed    
else:

    print('Ok!')

print ('')
print ('You have selected: ' + file_path)
print ('')


stop_words = set(stopwords.words("english") + list(string.punctuation))

filtered_tokens = []

for w in tokens:
   if w not in stop_words:
       filtered_tokens.append(w)

counts=Counter(filtered_tokens)


numOfWords = input('Would you like to continue to selecting the number of words to analyze? (yes or no): ')
if numOfWords == 'yes':

    root = tk.Tk()
    root.geometry("%dx%d+%d+%d" % (330, 80, 200, 150))
    root.title("Number of Words to Analyze, close window when finished")

    var = tk.StringVar(root)
    var.set("Select Num Here")

    def grab_and_assign(event):
        chosen_option = var.get()
        label_chosen_variable= tk.Label(root, text=chosen_option)
        label_chosen_variable.grid(row=1, column=2)
        print ('You selected ' + chosen_option)

    def leave():
        root.quit()
        root.destroy()

    drop_menu = tk.OptionMenu(root, var,  "100", "200", "300", "400", "500", command=grab_and_assign)
    drop_menu.grid(row=0, column=0)
    label_left=tk.Label(root, text="Chosen Num of Words= ")
    label_left.grid(row=1, column=0)
    label_right=tk.Button(root, text="Close", command=leave)
    label_right.grid(row=10, column=5)

    root.mainloop()


else:
    print ('Ok!')


then = input('Would you like to continue to the table creation? (yes or no): ')
if then == 'yes':
    word_freq = counts.most_common(int(var.get()))
    class Example(tk.Frame):
        def __init__(self, parent):
            tk.Frame.__init__(self, parent)
            b = tk.Button(self, text="Done!", command=self.upload_cor)
            b.grid()
        canvas=Canvas(self,bg='#FFFFFF',width=300,height=10000,scrollregion=(0,0,500,15000))
            vbar=Scrollbar(self,orient=VERTICAL)
            vbar.grid(row=1,column=1,sticky='nsw')
            vbar.config(command=canvas.yview)
            canvas.config(width=300,height=300)
            canvas.config(yscrollcommand=vbar.set)
            canvas.grid(row=1,column=0,sticky='news')
            table = tk.Frame(canvas, width=300, height=10000)


  canvas.create_window(0,0,anchor='nw',height=10000,width=300,window=table)

            data = (word_freq)

            self.widgets = {}
            row = 0
            for word, freq in (data):
                row += 1
                var1 = tk.IntVar()
                self.widgets[word] = {
                    "Word": tk.Label(table, text=word),
                    "Freq": tk.Label(table, text=freq),
                    "checkbox_var": var1,
                    "checkbox": tk.Checkbutton(table, variable=var1)
                }

                self.widgets[word]["Word"].grid(row=row, column=0, sticky="nsew")
                self.widgets[word]["Freq"].grid(row=row, column=1, sticky="nsew")

                self.widgets[word]["checkbox"].config(variable=var1)
                self.widgets[word]["checkbox"].grid(row=row, column=2, sticky="nsew")



            table.grid_columnconfigure(1, weight=1)
            table.grid_columnconfigure(2, weight=1)

        def upload_cor(self):
            for word in sorted(self.widgets.keys()):
                print(var1.get())

    if __name__ == "__main__":
        root = tk.Tk()
        Example(root).grid(sticky='nsew')
        root.mainloop()

else:    
    print ('Ok!')

缩进在我的文件中是正确的,但是其中一些搞砸了并修复它没有用完

0 个答案:

没有答案