如何定制软件包panastable,以便当我在显示中按下Return时,弹出一个警告。我尝试将Return绑定到函数callback,该函数将创建messagebox。但是什么也没发生。当我在单元格中输入新内容并按Return键后,它应该会给出警告。这是我使用的代码:
import pandas as pd
from pandastable import Table
import tkinter as tk
from tkinter import messagebox
class MyTable(Table):
def callback(self, event):
messagebox.showinfo(title="Achtung", message="Achtung")
def showWarning(self):
self.bind('<Return>', self.callback)
top = tk.Tk()
top.geometry("300x1000")
df = pd.DataFrame()
df["column1"] = [1,2,3,4,5]
frame = tk.Frame(top)
frame.pack(fill="both", expand=True)
pt = MyTable(frame, dataframe=df)
pt.show()
pt.focus_set()
pt.showWarning()
button = tk.Button(top, text="Änderungen speichern", command=top.quit)
button.pack()
top.mainloop()发布于 2021-10-30 18:25:38
因此,有一种更好的IMO方法(因为使用继承的东西):
import pandas as pd
from pandastable import Table
import tkinter as tk
from tkinter import messagebox
class MyTable(Table):
@staticmethod
def show_info():
messagebox.showwarning(title="Achtung", message="Achtung")
def handleCellEntry(self, row, col):
super().handleCellEntry(row, col)
self.show_info()
root = tk.Tk()
root.geometry("300x1000")
df = pd.DataFrame()
df["column1"] = [1, 2, 3, 4, 6]
frame = tk.Frame(root)
frame.pack(fill="both", expand=True)
pt = MyTable(frame, dataframe=df)
pt.show()
button = tk.Button(root, text="Änderungen speichern", command=root.quit)
button.pack()
root.mainloop()使用继承的方法handleCellEntry,该方法在编辑条目时按Enter键时调用,这正是您似乎需要的。您可以在source code中看到此方法如何绑定到特定条目的'<Return>'序列(所以只需覆盖它,调用超级方法(这样它就可以做它应该做的事情),然后调用您自己的方法(反之亦然,但是只有在关闭对话框之后,条目才会被更改)。
发布于 2021-10-30 06:10:47
解决方案是将其绑定到top window。下面的代码得到了想要的结果:
import pandas as pd
from pandastable import Table
import tkinter as tk
from tkinter import messagebox
top = tk.Tk()
top.geometry("300x1000")
df = pd.DataFrame()
df["column1"] = [1,2,3,4,5]
frame = tk.Frame(top)
frame.pack(fill="both", expand=True)
def callback(*args):
messagebox.showinfo(title="Achtung", message="Achtung")
top.bind('<Return>', callback)
pt = Table(frame, dataframe=df)
pt.show()
button = tk.Button(top, text="Änderungen speichern", command=top.quit)
button.pack()
top.mainloop()https://stackoverflow.com/questions/69773800
复制相似问题