Python 3.x 如何调用方法,使用按钮命令,其中im使用函数绑定?
我已经找到了这个代码在这里徘徊,但找不到办法问一个answears所以我在这里。所以这里的问题是绑定的工作方式正是我所需要的,但是在绑定之后,按钮失去了它的命令功能,在单击之后什么也不做。我该如何解决这个问题或以其他方式解决Python 3.x 如何调用方法,使用按钮命令,其中im使用函数绑定?,python-3.x,tkinter,Python 3.x,Tkinter,我已经找到了这个代码在这里徘徊,但找不到办法问一个answears所以我在这里。所以这里的问题是绑定的工作方式正是我所需要的,但是在绑定之后,按钮失去了它的命令功能,在单击之后什么也不做。我该如何解决这个问题或以其他方式解决 import tkinter as tk from tkinter import * def com(e): print("Ahoj") print(e) def on_enter(e): global d x, y = root.myBu
import tkinter as tk
from tkinter import *
def com(e):
print("Ahoj")
print(e)
def on_enter(e):
global d
x, y = root.myButton.winfo_rootx(), root.myButton.winfo_rooty()
x = x + root.myButton.winfo_width()
y = y + root.myButton.winfo_height()
d = Toplevel()
data="+{}+{}".format(x,y)
d.geometry(data)
d.overrideredirect(1)
L= tk.Label(d, text="Lorme ipsum lorem ipsum...", font=('Helvetica', 12))
L.pack()
d.mainloop()
def on_leave(e):
d.destroy()
root = tk.Tk()
root.myButton = tk.Button(root,text="Click Me", command=com)
root.myButton.grid()
root.myButton.bind("<Enter>", on_enter)
root.myButton.bind("<Leave>", on_leave)
root.mainloop()
将tkinter作为tk导入
从tkinter进口*
def com(e):
打印(“Ahoj”)
打印(e)
def on_输入(e):
全球d
x、 y=root.myButton.winfo_rootx(),root.myButton.winfo_rooty()
x=x+root.myButton.winfo_width()
y=y+root.myButton.winfo_height()
d=顶级()
data=“+{}+{}”。格式(x,y)
d、 几何(数据)
d、 重写直接(1)
L=tk.Label(d,text=“Lorme ipsum lorem ipsum…”,font=('Helvetica',12))
L.pack()
d、 mainloop()
休假期间的def(e):
d、 销毁
root=tk.tk()
root.myButton=tk.Button(root,text=“单击我”,command=com)
root.myButton.grid()
root.myButton.bind(“,在输入时)
root.myButton.bind(“,在左边)
root.mainloop()
这是我的一段主要代码(不想全部共享)对不起,我犯了太多错误
from tkinter import *
import tkinter as tk
class Page(tk.Tk):
def __init__(self, *ags, **kwargs):
tk.Tk.__init__(self)
self._frame=MyPage(self)
self._frame.grid()
class MyPage(tk.Frame):
buttons=[1,2,3,4,5,6]
buttons_store=[None, None, None, None, None, None]
def __init__(self, controller):
tk.Frame.__init__(self)
i=0
for button in self.buttons:
command = lambda x=button: self.select(x)
self.buttons_store[button-1]=tk.Button(self, text=button, command=command) # here is the command function
self.buttons_store[button-1].bind("<Enter>",lambda x, butt= button-1: self.on_enter(x))
self.buttons_store[button-1].bind("<Leave>",lambda x,butt= button-1: self.on_leave(x))
self.buttons_store[button-1].bind("<Button-1>",lambda x,butt= button-1: self.on_leave(x))
self.buttons_store[button-1].grid(sticky="WENS",ipadx=5, ipady=5,column=i, row=i)
i+=1
def select(self,x):
print(x)
def on_enter(self,e):
#print("im in") # This works
self.d= tk.Toplevel()# But if i add this piece buttton loses his command function
l= tk.Label(self.d,text="Ahoj" )
l.grid()
self.d.mainloop()
def on_leave(self,e):
#print("im out")# this works right
self.d.destroy()
app = Page()
app.mainloop()
从tkinter导入*
将tkinter作为tk导入
类页(tk.tk):
定义初始值(自我、*ags、**kwargs):
tk.tk.\uuuuu初始化(self)
self.\u frame=MyPage(self)
self.\u frame.grid()
类MyPage(tk.Frame):
按钮=[1,2,3,4,5,6]
按钮存储=[无,无,无,无,无,无,无]
定义初始化(自身,控制器):
tk.Frame.\uuuu init\uuuuu(自)
i=0
对于self.butions中的按钮:
命令=lambda x=按钮:self.select(x)
self.button_存储[button-1]=tk.button(self,text=button,command=command)#这里是命令函数
self.buttons\u存储[button-1].bind(“,lambda x,butt=button-1:self.on\u输入(x))
self.buttons\u存储[button-1].bind(“,lambda x,butt=button-1:self.on\u离开(x))
self.buttons\u存储[button-1].bind(“,lambda x,butt=button-1:self.on\u离开(x))
self.buttons\u存储[button-1]。网格(sticky=“WENS”,ipadx=5,ipady=5,column=i,row=i)
i+=1
def选择(自身,x):
打印(x)
def on_进入(自我,e):
#打印(“im in”)#这行得通
self.d=tk.Toplevel()#但如果我添加这一部分,Button将失去其命令功能
l=tk.Label(self.d,text=“Ahoj”)
l、 网格()
self.d.mainloop()
休假时的def(自我,e):
#打印(“即时输出”)#这是正确的
自我毁灭
app=第页()
app.mainloop()
在绑定下只有一个pupup窗口(topleLevel函数)您的
com()
函数有一个参数e
,并且命令的回调
选项不需要参数。因此,当您单击按钮并尝试执行该功能时会出现错误。此问题是否发生在名为mybutton
的按钮上?@DaniyalAhmad yes。它不调用它的函数。@acw1668是的,但点按钮甚至不调用函数。没有出现任何错误。单击“无任何事件”后,这可能是由于在[u enter()
函数上调用d.mainloop()
内部引起的。请删除该行,然后重试。