按钮执行命令一次(Tkinter)
Button excecutes command once (Tkinter)
对于屏幕键盘,我需要 26 个按钮,它们都将键盘的一个字母添加到一个字符串中。我遇到的第一个问题是按下按钮后字符串没有被保存。例如,用户按下 'A','A' 将被打印并应被存储。用户现在按下 'B','B' 被打印出来,'A' 消失了。第二个问题是按钮只在第一次执行该功能。到目前为止我的代码如下:
window = Tk()
window.attributes('-fullscreen', True)
window.configure(background='yellow')
master = Frame(window)
master.pack()
e = Entry(master)
e.pack()
e.focus_set()
nieuw_station = ""
def printanswer():
global nieuw_station
nieuw_station = e.get()
print(nieuw_station)
gekozen_station(nieuw_station)
invoeren = Button(master, text="Invoeren", width=10, command=printanswer)
invoeren.pack()
def letter_toevoegen(nieuw_station, letter):
nieuw_station += letter
print(nieuw_station)
return nieuw_station
a = Button(master, text="A", width=1, command=letter_toevoegen(nieuw_station, "a"))
a.pack()
b = Button(master, text="B", width=1, command=letter_toevoegen(nieuw_station, "b"))
b.pack()
window.mainloop()
预期输出为:用户按下 'A','A' 得到打印和存储。现在用户按下 'B','AB' 被打印和存储。最后用户按下 'C',现在打印并存储 'ABC'。每当用户按下 'invoeren' 按钮时,它将以新字符串作为参数发送到下一个函数(这实际上有效)。
你遇到了一些问题。首先,command
希望您传递一个 函数 ,但您传递的是一个函数 的 return 值。一种解决方法是将其包装在 lambda
:
中
a = Button(master, text="A", width=1, command=lambda: letter_toevoegen(nieuw_station, "a"))
a.pack()
其次,我不知道 nieuw_station.close()
和 nieuw_station.open()
在做什么 -- 字符串没有这些方法。
第三,字符串是不可变的,因此当您在 letter_toevoegen
中执行 nieuw_station += letter
时,该更改不会在函数外持续存在。
真正让它持久化的一种方法是使用global nieuw_station
——然后你可能无法将它传递给函数。
话虽如此,当您看到 global
语句时,10 次中有 9 次是使用 classes 的更好方法。在这里,我们可以创建一个 class 添加按钮并跟踪状态。
class App(object):
def __init__(self):
self.nieuw_station = ''
def add_buttons(self):
a = Button(master, text="A", width=1, command=lambda: self.letter_toevoegen("a"))
a.pack()
# ...
def letter_toevoegen(self, letter):
time.sleep(2)
self.nieuw_station += letter
print(self.nieuw_station)
return self.nieuw_station
当然,我们也会在此处添加 printanswer
并添加 "print answer" 按钮等。使用 class 的代码如下所示:
app = App()
app.add_buttons()
window.mainloop()
您会在此处看到许多变体。 (有些人喜欢 App
继承自 tkinter.Tk
或其他一些 tkinter 小部件,其他人会将父小部件传递给 class 以便它可以知道将所有元素附加到哪里,等等.) 我 试图 展示的要点是如何使用 class 作为数据容器而不是全局命名空间。
对于屏幕键盘,我需要 26 个按钮,它们都将键盘的一个字母添加到一个字符串中。我遇到的第一个问题是按下按钮后字符串没有被保存。例如,用户按下 'A','A' 将被打印并应被存储。用户现在按下 'B','B' 被打印出来,'A' 消失了。第二个问题是按钮只在第一次执行该功能。到目前为止我的代码如下:
window = Tk()
window.attributes('-fullscreen', True)
window.configure(background='yellow')
master = Frame(window)
master.pack()
e = Entry(master)
e.pack()
e.focus_set()
nieuw_station = ""
def printanswer():
global nieuw_station
nieuw_station = e.get()
print(nieuw_station)
gekozen_station(nieuw_station)
invoeren = Button(master, text="Invoeren", width=10, command=printanswer)
invoeren.pack()
def letter_toevoegen(nieuw_station, letter):
nieuw_station += letter
print(nieuw_station)
return nieuw_station
a = Button(master, text="A", width=1, command=letter_toevoegen(nieuw_station, "a"))
a.pack()
b = Button(master, text="B", width=1, command=letter_toevoegen(nieuw_station, "b"))
b.pack()
window.mainloop()
预期输出为:用户按下 'A','A' 得到打印和存储。现在用户按下 'B','AB' 被打印和存储。最后用户按下 'C',现在打印并存储 'ABC'。每当用户按下 'invoeren' 按钮时,它将以新字符串作为参数发送到下一个函数(这实际上有效)。
你遇到了一些问题。首先,command
希望您传递一个 函数 ,但您传递的是一个函数 的 return 值。一种解决方法是将其包装在 lambda
:
a = Button(master, text="A", width=1, command=lambda: letter_toevoegen(nieuw_station, "a"))
a.pack()
其次,我不知道 nieuw_station.close()
和 nieuw_station.open()
在做什么 -- 字符串没有这些方法。
第三,字符串是不可变的,因此当您在 letter_toevoegen
中执行 nieuw_station += letter
时,该更改不会在函数外持续存在。
真正让它持久化的一种方法是使用global nieuw_station
——然后你可能无法将它传递给函数。
话虽如此,当您看到 global
语句时,10 次中有 9 次是使用 classes 的更好方法。在这里,我们可以创建一个 class 添加按钮并跟踪状态。
class App(object):
def __init__(self):
self.nieuw_station = ''
def add_buttons(self):
a = Button(master, text="A", width=1, command=lambda: self.letter_toevoegen("a"))
a.pack()
# ...
def letter_toevoegen(self, letter):
time.sleep(2)
self.nieuw_station += letter
print(self.nieuw_station)
return self.nieuw_station
当然,我们也会在此处添加 printanswer
并添加 "print answer" 按钮等。使用 class 的代码如下所示:
app = App()
app.add_buttons()
window.mainloop()
您会在此处看到许多变体。 (有些人喜欢 App
继承自 tkinter.Tk
或其他一些 tkinter 小部件,其他人会将父小部件传递给 class 以便它可以知道将所有元素附加到哪里,等等.) 我 试图 展示的要点是如何使用 class 作为数据容器而不是全局命名空间。