Python Forum
Displaying database info in QTableWidget
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Displaying database info in QTableWidget
#2
Your question is boring, so I am going to answer a different question.

Views that need to update to reflect changes in data should have an update method. This update method is bound to a notify event associated with the data. When the data changes it sends notification to all the interested parties. Something like this:
import tkinter as tk

class CallbackList(list):
    """A list of functions to call"""
    def __call__(self, value):
        for func in self:
            func(value)

    def connect(self, func):
        """Add function to the list"""
        self.append(func)

class MyData():
    """This is my data"""
    def __init__(self):
        self.value = 1
        self.value_changed = CallbackList()
    
    def set_value(self, value):
        """Set my data and notify that value changed"""
        self.value = value
        self.value_changed(self.value)

def enter_value(event):
    """Gets called when Retrun key is pressed in entry widget"""
    data.set_value(float(event.widget.get()))

def update_square(value):
    """Call this when value changes"""
    square['text'] = str(value**2)

def update_cube(value):
    """Call this when value changes"""
    cube['text'] = str(value**3)

data = MyData()

root = tk.Tk()
entry = tk.Entry(root, width=4)
entry.pack()
entry.bind('<Return>', enter_value)

square = tk.Label(root, width=4)
square.pack()
data.value_changed.connect(update_square)

cube = tk.Label(root, width=4)
cube.pack()
data.value_changed.connect(update_cube)
root.mainloop()
You want to avoid having one control being directly responsible for changing another control. Try to make each GUI element responsible for doing only one thing, and have that control and associated control do it's job while being as ignorant as possible about other parts of the GUI.

Back to your question. Where code exists should have no affect on if arguments can be passed. If you get something from a database in one file, there is no reason why that information cannot be passed as an argument to a function in a different file that displays the information. I think any limitations you see are not real. You think there is something special about code being in different files. The only thing affected by that are module variables, and you should not be using many of those.
thewolf likes this post
Reply


Messages In This Thread
RE: Displaying database info in QTableWidget - by deanhystad - Mar-27-2021, 05:11 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PyQt5 form not displaying my data from SQLite3 Database Linuxdesire 2 5,059 Jan-10-2023, 09:51 PM
Last Post: gradlon93
  [PyQt] [Solved]Help displaying SQLite Database Extra 9 2,900 May-21-2022, 08:03 PM
Last Post: Extra
  [PyQt] QTableWidget print problem JokerSob 8 4,922 Jan-28-2022, 06:08 PM
Last Post: Axel_Erfurt
  [PyQt] How do I display the DB table I will choose from the QComboBox in QTableWidget JokerSob 2 2,373 Aug-05-2021, 03:00 PM
Last Post: JokerSob
  [PyQt] Help: check content of combobox in horizontal header of QTableWidget mart79 1 3,444 Jul-26-2020, 06:18 PM
Last Post: deanhystad
  [PyQt] Add validation (regex) to QTableWidget cells mart79 0 2,803 May-05-2020, 12:51 PM
Last Post: mart79
  [Tkinter] Displaying Data from a database and run a function when clicked? PythonNPC 1 2,111 Mar-11-2020, 08:16 PM
Last Post: Larz60+
  [PyQt] QTableWidget stylesheet error mart79 3 6,548 Feb-26-2020, 04:54 PM
Last Post: Denni
  [PyQt] Pyqt5: How do you make a button that adds new row with data to a Qtablewidget YoshikageKira 6 7,184 Jan-02-2020, 04:32 PM
Last Post: Denni
  [PyQt] QTableWidget cell validation littleGreenDude 1 7,747 Jan-18-2019, 07:44 PM
Last Post: littleGreenDude

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020