gpt4 book ai didi

Python 2.7.2 全局变量 + tkinter

转载 作者:行者123 更新时间:2023-11-28 23:04:13 25 4
gpt4 key购买 nike

我在使用全局变量时遇到一些问题:

#Dev by Mkee
import Tkinter as tk
import sys, random
globvar = 0
class Shuffle(object):
def __init__(self,master=None):
self.master=master
self.text=tk.Label(master, text="Type in something:")
self.box=tk.Entry(master)
self.buttn = tk.Button(master, text="Done", width=10, command=self.calc())
self.result = tk.Label(self.master, text=globvar)
def hide(self):
self.text.pack_forget()
self.box.pack_forget()
self.buttn.pack_forget()
self.result.pack_forget()
def show(self):
self.text.pack(padx=2, pady=2)
self.box.focus_set()
self.box.pack(side=tk.TOP, padx=3, pady=3)
self.buttn.pack(side=tk.TOP, padx=3, pady=3)
self.result.pack(side=tk.TOP, padx=3, pady=3)
def calc(self):
word = self.box.get()
while len(word) != 0:
i = random.randrange(0, len(word))
global globvar
globvar = word[i]
word = word[0:i] + word[i+1:]


class MainApp(object):
def __init__(self,master=None):
self.master=master
app=self.app=tk.Tk()
app.title("Mkee's Tools")
app.geometry('500x500')
menubar=tk.Menu(app)
self.shuffle=Shuffle(master)
self.current=self.shuffle
program_menu=tk.Menu(menubar)
program_menu.add_command(label='Shuffle',command=self.show_shuffle)
menubar.add_cascade(label='Programs', menu=program_menu)
app.config(menu=menubar)
def show_shuffle(self):
self.current.hide()
self.current=self.shuffle
self.shuffle.show()
def show_buttons(self):
self.current.hide()
self.current=self.buttons
self.buttons.show()

def main():
m=MainApp()
m.app.mainloop()

if __name__=='__main__':
main()

当我点击按钮时出现问题self.buttn = tk.Button(master, text="Done", width=10, command=self.calc())。我想让 self.result = tk.Label(self.master, text=globvar) 显示从 calc 中获取的 globvar,而是显示在类 之前设置的 globvar globvar = 0。我做错了什么?

最佳答案

  1. 要更新 Label,设置 textvariable=self.globvar,其中self.globvar = tk.StringVar()。通过调用 self.globvar.set 更新 Label 文本。
  2. command=self.calc() 更改为 command=self.calc(没有括号)。括号内是当时调用的self.calc()self.buttn 已定义。并且命令被设置为返回值self.calc(),即None。按下按钮时没有任何反应按下是因为 command 等于 None。没有括号,command 设置为函数对象 self.calc,并且只要按下按钮,就会(正确地)调用函数。
  3. 尽量避免使用全局变量。它将帮助 Shuffle 成为独立的,更容易重用和导入。

import Tkinter as tk
import sys, random

class Shuffle(object):
def __init__(self,master=None):
self.master=master
self.text=tk.Label(master, text="Type in something:")
self.box=tk.Entry(master)
self.buttn = tk.Button(master, text="Done", width=10, command=self.calc)
self.globvar = tk.StringVar()
self.globvar.set(0)
self.result = tk.Label(self.master, textvariable=self.globvar)
def hide(self):
self.text.pack_forget()
self.box.pack_forget()
self.buttn.pack_forget()
self.result.pack_forget()
def show(self):
self.text.pack(padx=2, pady=2)
self.box.focus_set()
self.box.pack(side=tk.TOP, padx=3, pady=3)
self.buttn.pack(side=tk.TOP, padx=3, pady=3)
self.result.pack(side=tk.TOP, padx=3, pady=3)
def calc(self):
word = self.box.get()
while len(word) != 0:
i = random.randrange(0, len(word))
self.globvar.set(word[i])
word = word[0:i] + word[i+1:]

class MainApp(object):
def __init__(self,master=None):
self.master=master
app=self.app=tk.Tk()
app.title("Mkee's Tools")
app.geometry('500x500')
menubar=tk.Menu(app)
self.shuffle=Shuffle(master)
self.current=self.shuffle
program_menu=tk.Menu(menubar)
program_menu.add_command(label='Shuffle',command=self.show_shuffle)
menubar.add_cascade(label='Programs', menu=program_menu)
app.config(menu=menubar)
def show_shuffle(self):
self.current.hide()
self.current=self.shuffle
self.shuffle.show()
def show_buttons(self):
self.current.hide()
self.current=self.buttons
self.buttons.show()

def main():
m=MainApp()
m.app.mainloop()

if __name__=='__main__':
main()

关于Python 2.7.2 全局变量 + tkinter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7865682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com