将参数传递给tkinter中的函数

时间:2018-05-24 04:03:24

标签: python tkinter lambda

我正在尝试设置一个GUI功能,允许用户点击按钮,然后在类别'名词','动词','形容词'等上输入/输入新单词。我有的程序设置保存在与程序相同的目录中的引用.txt文件。

我无法将参数传递给按钮,我发现的答案建议在some simple situations中使用lambda函数。如果我删除所有传递的参数并简单地在每个函数中分配一个特定的文件(farsi_nouns等),该程序就可以工作。我无法判断我是否错误地执行此操作,或者Tkinter是否过于基本的GUI以这种方式将参数传递给函数。非常感谢任何反馈!

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

1 个答案:

答案 0 :(得分:0)

如果将代码放在类中,则可以轻松传递参数。另一件事是你可以尝试的tkinters函数.after()。我已经制作了一个简单的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()