0

I'm prepared two files. First with GUI class. Second file is a script where I want to use a GUI, Databases and Template classes. A Databases and Template classes works. I check it in other scripts.

Questions: How can I use event(KeyRelase) form GUI class in script?

First:

from tkinter import *
from tkinter.font import Font
from tkinter.ttk import Separator


class GUI:

def __init__(self, master):
    self.ent_InventoryNumber = Entry(self.frm_FirstColumn, font=fontStyle, width=35, borderwidth=2, justify=CENTER)
    self.ent_InventoryNumber.insert(END, "Wprowadź TUTAJ numer inwentarzowy")
    self.ent_InventoryNumber.grid(row=2, column=0, columnspan=2, padx=5, pady=20, ipadx=2, ipady=2)
    self.ent_InventoryNumber.bind("<KeyRelease>", lambda x: self.searchChamber())


def searchChamber(self):
   return self.ent_InventoryNumber.get()

Second file with script:

import Databases as Db
import Template
from GUI import GUI

def test(a):
   print(a)

window = Tk()
myGUI = GUI(window)
window.mainloop()
# test(myGUI.searchChamber()) ???
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • 1
    First you have to understand [Event-driven programming](https://stackoverflow.com/a/9343402/7414759) it is unrelated in which file or part of code the `.bind(...` is defined. – stovfl Mar 30 '20 at 12:13
  • `bind` can execute function but it can't use value returned from this function - so using `return` is useless here. Better use `print(self.ent_InventoryNumber.get())` or assign it to some value - ie. `self.value = self.ent_InventoryNumber.get()` - to use it later. OR use this value directly in `searchChamber()` – furas Mar 30 '20 at 12:24
  • PL: `bind` potrafi wywołać funkcję ale nie potrafi odebrać wartość zwracanej przez tą funkcję (nie ma do czego przypisać tej wartości) więc użycie `return` w `searchChamber` jest bezużyteczne. Musisz tę wartość przypisać do jakiejś zmiennej albo użyć od razu wewnątrz `searchChamber`. BTW: ten sam problem będziesz miał gdy cały kod wrzucisz do jednego pliku - więc problem nie jest jak użyć bind w drugim pliku ale jak w ogóle używać `bind`. – furas Mar 30 '20 at 12:27
  • BTW: your code works for me. Do you get any error message or what? always put full error message (starting at word "Traceback") in question (not comment) as text (not screenshot). There are other useful information. – furas Mar 30 '20 at 12:43

1 Answers1

0

bind works correctly in you code. Problem is that bind can't get value which you use with return - it can't assing it to any variable and you have to use this value directly in searchChamber or assign to some variable to use it later.

Other problem: when you close window then tkinter destroy all widgets and you have to keep value from Entry in some variable

    def searchChamber(self):
        self.result = self.ent_InventoryNumber.get()

and later get this variable

    test(GUI.result)

In this example I use print() to see if bind executes function after every pressed key. I also use class variable self.result to keep value and use it after closing window.

GUI.py

import tkinter as tk

class GUI:

    def __init__(self, master):
        self.result = '' # default value as start

        self.ent_InventoryNumber = tk.Entry(master)
        self.ent_InventoryNumber.insert('end', "Wprowadź TUTAJ numer inwentarzowy")
        self.ent_InventoryNumber.grid(row=2, column=0)
        self.ent_InventoryNumber.bind("<KeyRelease>", self.searchChamber)

    def searchChamber(self, event=None):
        self.result = self.ent_InventoryNumber.get()

        print('[DEBUG] searchChamber:', self.result)

main.py

import tkinter as tk
from GUI import GUI

window = tk.Tk()
myGUI = GUI(window)
window.mainloop()
print('result:', myGUI.result)
furas
  • 134,197
  • 12
  • 106
  • 148