简体   繁体   English

将参数传递给tkinter中的函数

[英]Passing arguments to functions in tkinter

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. 我正在尝试建立一个GUI功能,该功能允许用户单击按钮,然后在“名词”,“动词”,“形容词”等类别中被测验/输入新单词。 .txt文件保存在与程序相同的目录中。

I haven't been able to pass arguments to buttons, and the answers I've found suggest using lambda functions in some simple situations . 我一直无法将参数传递给按钮,而我发现的答案建议在某些简单情况下使用lambda函数。 The program works if I remove all passed arguments and simply assign a specific file (farsi_nouns, etc.) within each function. 如果我删除所有传递的参数,并在每个函数中简单地分配一个特定文件(farsi_nouns等),则该程序将起作用。 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. 我无法判断我是否做错了,或者Tkinter是否太基本了,GUI无法以这种方式将参数传递给函数。 Thanks very much for any feedback! 非常感谢您的任何反馈!

Tkinter ver. Tkinter版本 8.5, Python 3.5.2, OSx High Sierra 10.13.4. 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()

You can pass arguments easily if you put your code in a class. 如果将代码放在类中,则可以轻松传递参数。 Another thing is the tkinters function .after() you can try. 另一件事是tkinters函数.after .after()可以尝试。 I have made a simple GUI for demonstration of both. 我制作了一个简单的GUI来演示两者。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM