Home > other >  Creating popup using Return in pandastable
Creating popup using Return in pandastable

Time:10-31

How can I customize the package panastable so that when i press Return in the display, a warning pops up. I tried to bind Return to the function callback, which should create the messagebox. But nothing happens. It should give the warning after I enter new content in the cell and press Return. This is code I'm using:

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()

CodePudding user response:

The solution was binding it the top window. The following code get the wanted result:

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()

CodePudding user response:

So there is a much better approach to this IMO (because using what is inherited):

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()

Using the inherited method handleCellEntry which is called when enter key is pressed while editing the entry, just what you seem to need. You can see in the source code how this method is bound to '<Return>' sequence to that specific entry (so just override it, call the super method (so it does what it is supposed to do) and then your own method (or vice versa but then the entry will be changed only after you close the dialog)).

  • Related