Python 使用事件将键盘键绑定到小部件
当我尝试绑定键并启动脚本时,该键自动按下。 如何使用事件等待按键或提交按钮被按下 我在运行时遇到此错误:Python 使用事件将键盘键绑定到小部件,python,user-interface,tkinter,key-bindings,Python,User Interface,Tkinter,Key Bindings,当我尝试绑定键并启动脚本时,该键自动按下。 如何使用事件等待按键或提交按钮被按下 我在运行时遇到此错误: 通过将等待表达的“lambda”更改为“lambda e:” class CmdCli(tk.Frame): file = fr'c:/users/{os.getlogin()}/Desktop/default.txt' newfile = fr'c:/users/{os.getlogin()}/Desktop/Gui-Skeleton/default-tmp.txt'
通过将等待表达的“lambda”更改为“lambda e:”
class CmdCli(tk.Frame):
file = fr'c:/users/{os.getlogin()}/Desktop/default.txt'
newfile = fr'c:/users/{os.getlogin()}/Desktop/Gui-Skeleton/default-tmp.txt'
lastfile = fr'c:/users/{os.getlogin()}/Desktop/logger.txt'
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, bg="skyblue1")
self.controller = controller
self.command_label = tk.Label(self, text="Enter Command : ", padx=7, pady=5, bg="skyblue1")
self.command_input_box = tk.Entry(self)
self.command_input_box.bind('<Return>', lambda e: self.thread_handler(self.command_input_box.get()))
self.submit_button = tk.Button(self, text="Submit", width=10, height=1,
command=lambda: self.thread_handler(self.command_input_box.get()))
self.clear_field_button = tk.Button(self, text="Clear Field", width=10, padx=2,
command=lambda: self.clear_boxes(self.command_input_box))
self.clear_file_button = tk.Button(self, text="Clear File", width=10,
command=lambda:
open(self.lastfile, 'w+').close())
self.clear_window_button = tk.Button(self, text="Clear Window", width=10,
command=lambda: self.clean_window())
self.stop_button = tk.Button(self, text="Stop", padx=20, command=lambda: self.stop_process())
self.grid_columnconfigure(0, minsize=20, weight=0)
self.grid_columnconfigure(1, minsize=20, weight=2)
self.grid_columnconfigure(2, minsize=5, weight=3)
self.grid_columnconfigure(3, minsize=20, weight=2)
self.grid_rowconfigure(0, minsize=50, weight=0)
self.grid_rowconfigure(1, minsize=20, weight=0)
self.grid_rowconfigure(2, minsize=30, weight=0)
self.grid_rowconfigure(3, minsize=10, weight=0)
self.grid_rowconfigure(4, minsize=10, weight=0)
self.grid_rowconfigure(5, minsize=10, weight=0)
self.command_label.grid(row=0, column=1, sticky="nsew")
self.command_input_box.grid(row=1, column=1, sticky="nsew")
self.clear_window_button.grid(row=3, column=1, sticky="w")
self.clear_file_button.grid(row=3, column=1, sticky="n")
self.clear_field_button.grid(row=1, column=1, sticky="e")
self.submit_button.grid(row=3, column=1, sticky="se")
self.stop_button.place(x=510, y=106)
class CmdCli(tk.Frame):
file=fr'c:/users/{os.getlogin()}/Desktop/default.txt'
newfile=fr'c:/users/{os.getlogin()}/Desktop/Gui-Skeleton/default tmp.txt'
lastfile=fr'c:/users/{os.getlogin()}/Desktop/logger.txt'
定义初始化(自、父、控制器):
tk.Frame.\uuuuu init\uuuuuuuuuuuuuu(self,parent,bg=“skyblue1”)
self.controller=控制器
self.command_label=tk.label(self,text=“Enter command:”,padx=7,pady=5,bg=“skyblue1”)
self.command\u input\u box=tk.Entry(self)
self.command\u input\u box.bind(“”,lambda e:self.thread\u处理程序(self.command\u input\u box.get()))
self.submit_按钮=tk.按钮(self,text=“submit”,宽度=10,高度=1,
command=lambda:self.thread\u处理程序(self.command\u input\u box.get())
self.clear\u field\u button=tk.button(self,text=“clear field”,width=10,padx=2,
command=lambda:self.clear_框(self.command_输入_框))
self.clear\u file\u button=tk.button(self,text=“clear file”,width=10,
命令=lambda:
打开(self.lastfile“w+).close())
self.clear\u window\u button=tk.button(self,text=“clear window”,width=10,
command=lambda:self.clean_window())
self.stop\u button=tk.button(self,text=“stop”,padx=20,command=lambda:self.stop\u process())
self.grid\u columnconfigure(0,minsize=20,weight=0)
self.grid\u columnconfigure(1,minsize=20,weight=2)
self.grid\u columnconfigure(2,minsize=5,weight=3)
self.grid\u columnconfigure(3,minsize=20,weight=2)
self.grid_rowconfigure(0,minsize=50,weight=0)
self.grid_rowconfigure(1,minsize=20,weight=0)
self.grid_rowconfigure(2,minsize=30,weight=0)
self.grid_rowconfigure(3,minsize=10,weight=0)
self.grid_rowconfigure(4,minsize=10,weight=0)
self.grid_rowconfigure(5,minsize=10,weight=0)
self.command\u label.grid(行=0,列=1,sticky=“nsew”)
self.command\u input\u box.grid(行=1,列=1,sticky=“nsew”)
self.clear\u window\u button.grid(行=3,列=1,sticky=“w”)
self.clear\u file\u button.grid(行=3,列=1,sticky=“n”)
self.clear\u field\u button.grid(行=1,列=1,sticky=“e”)
self.submit_button.grid(行=3,列=1,sticky=“se”)
自动停止按钮位置(x=510,y=106)
再次感谢acw1668 请不要张贴代码或堆栈跟踪的图片。这样更好吗?您尝试过使用lambda吗?像这样:
self.url\u input\u box.bind(“,lambda:self.thread\u handler(self.url\u input\u box.get())
?是的,我得到一个错误:TypeError:()接受0个位置参数,但1被赋予了bind
的回调需要一个参数,即事件对象。所以应该是self.url\u input\u box.bind(“,lambda e:…)
。
class CmdCli(tk.Frame):
file = fr'c:/users/{os.getlogin()}/Desktop/default.txt'
newfile = fr'c:/users/{os.getlogin()}/Desktop/Gui-Skeleton/default-tmp.txt'
lastfile = fr'c:/users/{os.getlogin()}/Desktop/logger.txt'
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, bg="skyblue1")
self.controller = controller
self.command_label = tk.Label(self, text="Enter Command : ", padx=7, pady=5, bg="skyblue1")
self.command_input_box = tk.Entry(self)
self.command_input_box.bind('<Return>', lambda e: self.thread_handler(self.command_input_box.get()))
self.submit_button = tk.Button(self, text="Submit", width=10, height=1,
command=lambda: self.thread_handler(self.command_input_box.get()))
self.clear_field_button = tk.Button(self, text="Clear Field", width=10, padx=2,
command=lambda: self.clear_boxes(self.command_input_box))
self.clear_file_button = tk.Button(self, text="Clear File", width=10,
command=lambda:
open(self.lastfile, 'w+').close())
self.clear_window_button = tk.Button(self, text="Clear Window", width=10,
command=lambda: self.clean_window())
self.stop_button = tk.Button(self, text="Stop", padx=20, command=lambda: self.stop_process())
self.grid_columnconfigure(0, minsize=20, weight=0)
self.grid_columnconfigure(1, minsize=20, weight=2)
self.grid_columnconfigure(2, minsize=5, weight=3)
self.grid_columnconfigure(3, minsize=20, weight=2)
self.grid_rowconfigure(0, minsize=50, weight=0)
self.grid_rowconfigure(1, minsize=20, weight=0)
self.grid_rowconfigure(2, minsize=30, weight=0)
self.grid_rowconfigure(3, minsize=10, weight=0)
self.grid_rowconfigure(4, minsize=10, weight=0)
self.grid_rowconfigure(5, minsize=10, weight=0)
self.command_label.grid(row=0, column=1, sticky="nsew")
self.command_input_box.grid(row=1, column=1, sticky="nsew")
self.clear_window_button.grid(row=3, column=1, sticky="w")
self.clear_file_button.grid(row=3, column=1, sticky="n")
self.clear_field_button.grid(row=1, column=1, sticky="e")
self.submit_button.grid(row=3, column=1, sticky="se")
self.stop_button.place(x=510, y=106)