0

I'm trying to set up a GUI feature that allows users to click a button, then be quizzed on/input new words in the categories 'nouns,' 'verbs,' 'adjectives,' etc. The program I have set up references .txt files saved in the same directory as the program.

I haven't been able to pass arguments to buttons, and the answers I've found suggest using lambda functions in some simple situations. The program works if I remove all passed arguments and simply assign a specific file (farsi_nouns, etc.) within each function. I'm not able to tell if I'm doing this incorrectly, or if Tkinter is too basic a GUI to pass arguments to functions in this way. Thanks very much for any feedback!

Tkinter ver. 8.5, Python 3.5.2, OSx High Sierra 10.13.4.

file_in_use = 'farsi_words'

def defaultFile(filename):
    file_in_use = filename
    return file_in_use

bN = Button(f0, text = 'Nouns', command =lambda: defaultFile('farsi_words'))
bN.pack(side='left')
bN.bind("<Button-1>",
bV = Button(f0, text = 'Verbs', command =lambda: defaultFile('farsi_verbs'))
bV.pack(side='left')
bA = Button(f0, text = 'Adjectives', command =lambda: defaultFile('farsi_adjectives'))
bA.pack(side='left')
bP = Button(f0, text = 'Prepositions', command =lambda: defaultFile('farsi_preps'))
bP.pack(side='left')

def commit(file_in_use):
    word = e1.get()
    definition = e2.get()
    appendFile = open(file_in_use, 'a')#was this defined before def? 
    appendFile.write('\n' + word + ': ' + definition)
    appendFile.close()
    e1.delete(0, 'end')
    e2.delete(0, 'end')

def review(file_in_use):
    t1.delete('1.0', END)
    readFile = open(file_in_use, 'r') 
    size = 0
    splitList = []
    for line in readFile:
        splitWord = line.split(':')
        splitWord = splitWord[0].strip('\n ')
        splitList.append(splitWord)
        size += 1

    n = random.randint(0, size - 1)
    t1.insert(INSERT, splitList[n] + '\n')
    readFile.close()

def answer(file_in_use):
    word = e3.get()
    def1 = t1.get('1.0','end-1c')
    def1 = def1.strip('\n')
    readFile = open(file_in_use, 'r') 
    for line in readFile:
        splitWord = line.split(': ')
        if def1 == splitWord[0].strip('\n'):
            if word == splitWord[1].strip('\n'):
                t1.insert(INSERT, 'Good job!')
            else:
                t1.insert(INSERT, 'Not quite! Good try =)')
    readFile.close()

def hint(file_in_use):
    def1 = t1.get('1.0','2.0')
    def1 = def1.strip('\n')
    readFile = open(file_in_use, 'r')

    for line in readFile:
        splitWord = line.split(': ')
        if def1 == splitWord[0].strip('\n'):
            hint = splitWord[1]

    hint1 = t1.get('2.0','end-1c')
    lenHint1 = len(hint1)
    if lenHint1 >= len(hint):
        pass
    else:
        t1.insert(INSERT, hint[lenHint1])
        print (hint1)
    readFile.close()
martineau
  • 119,623
  • 25
  • 170
  • 301
dia
  • 1
  • 1
  • There's a technique sometimes called [**The extra arguments trick**](http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/extra-args.html) which should help. You can also use `functools.partial()` to do the same thing. – martineau May 24 '18 at 04:28
  • 1
    I see nothing wrong with the way you are using arguments. When I run the code, the function gets the arguments passed. Why do you say it's not working? I don't think your problem is in passing values, it's in the fact that you don't do anything with the values in the function other than to set the value to a local variable. – Bryan Oakley May 24 '18 at 12:10

1 Answers1

0

You can pass arguments easily if you put your code in a class. Another thing is the tkinters function .after() you can try. I have made a simple GUI for demonstration of both.

import tkinter as tk
from tkinter import *


class GUI:

    def __init__(self, master):
        self.file_in_use = 'farsi_words'
        self.master = master
        self.bN = Button(master, text = 'Nouns', command = self.farsi_words)
        self.bN.pack(side='left')
        self.bN.bind("<Button-1>")
        self.bV = Button(master, text = 'Verbs', command = self.farsi_verbs)
        self.bV.pack(side='left')
        self.bA = Button(master, text = 'Adjectives', command = self.farsi_adjectives)
        self.bA.pack(side='left')
        self.bP = Button(master, text = 'Prepositions', command = self.farsi_preps)
        self.bP.pack(side='left')

    def farsi_words(self, event=None):
        self.file_in_use = 'Nouns'
        self.master.after(1, self.commit)

    def farsi_verbs(self, event=None):
        self.file_in_use = 'Verbs'
        self.master.after(1, self.commit)

    def farsi_adjectives(self, event=None):
        self.file_in_use = 'Adjectives'
        self.master.after(1, self.commit)

    def farsi_preps(self, event=None):
        self.file_in_use = 'Prepositiones'
        self.master.after(1, self.commit)

    def commit(self, event=None):
        print(self.file_in_use)

if __name__ == "__main__":
    root = Tk()
    my_gui = GUI(root)
    root.mainloop()
kavko
  • 2,751
  • 13
  • 27