0

Please can someone help here? The code should automatically transfer changes made in the UserID class (i.e. pressing the Delete button) to the RegisterUser class. So I can program there changes depending on the actions in the UserID class. All functions for this in the UserID class are also executed (I checked by print commands in the terminal). What does not arrive is the transfer to the RegisterUser class. And I don't know why.

Relevant code from RegisterUser class:

import flet as ft
from microservices.user_id import UserID

class RegisterUser(ft.UserControl):

    def __init__(self, user_id):
        
        super().__init__()

        self.user_id = user_id
        
        self.user_id.on_changed = self.on_changed

   def buil(self):
    ...

   return ...

   def on_changed(self, variable):
        print(f"user_id changed to {variable}")

relevant code from UserID class:

import flet as ft

class UserID(ft.UserControl):
    
    def __init__(self):
        super().__init__()

        self.on_changed = lambda x: None
        self.variable = None
        
    def build(self):

    ...

    self.user_textfield_row = ft.Row(
            spacing=10,
            controls=[
            self.user_textfield,
            ft.IconButton(ft.icons.DELETE_OUTLINE,tooltip="delete entry", 
            icon_color="#567189", icon_size = 30, on_click = self.delete_clicked),
            ft.IconButton(ft.icons.CHECK_CIRCLE_OUTLINED, tooltip="submit your ID", 
            icon_size=30, icon_color ="#567189", on_click=self.on_user_submit)  
            ],
            alignment=ft.MainAxisAlignment.CENTER
        )
       return: ...

       def set_variable(self, value):
            
            self.variable = value
            print(value)
            self.start()
        
       def start(self):
            if self.on_changed:
                self.on_changed(self.variable)
                print(self.on_changed)
        
       def delete_clicked(self, e):
            self.date_textfield.value =""
            print("Doing something...")
            variable = self.user_textfield.value
            self.user_textfield.value = ""
            self.user_textfield.focus()
            self.something = "delete clicked"
            self.set_variable(value=self.something)

I've been trying for days and just can't find a solution. Thanks a lot

i also tried to write a callback function. But unfortunately also unsuccessful. The code should automatically transfer a change of the UserID class to the RegisterUser class and imply further changes there.

  • What is `RegisterUser` supposed to contain? If changes to one table require changes to another table, then your database design is faulty. If "registered" is a part of a user's state, then it should be a field in the `UserID` class. I don't see code in your `delete_clicked` trying to delete anything in `RegisterUser`. – Tim Roberts Apr 10 '23 at 18:27
  • It should work in such a way that only the type of change is transferred. RegisterUser contains e.g. a Next button which is only released if a user input is made in UserID (either as Enter input or by clicking the Submit button), the other way round the Next button should be disabled as soon as the user deletes his input (i.e. corrects it). In addition, the date and time should be recorded by the RegisterUser class when the input is successful. delete_clicked should only clear the text field and then transfer the information about what the user has done to RegisterUser. – klettermax Apr 10 '23 at 18:39
  • I'm sorry -- I misread your code. I thought I was seeing database models, not UI controls. That's why my comment seemed like nonsense. – Tim Roberts Apr 10 '23 at 18:41
  • no it does not. I am happy to answer questions and explain what the code is supposed to do. I really need some help here. And if questions help with that, gladly anytime. – klettermax Apr 10 '23 at 18:43
  • The issue here is that you don't want to notify the `RegisterUser` class -- you need to notify an OBJECT of class `RegisterUser`, and to do that, the `UserID` object needs to be given that object. That has to be done by some object that owns both types. Do you have a controller class that can connect the two? Like, have it create a `RegisterUser` object, and pass that to the `UserID`? – Tim Roberts Apr 10 '23 at 18:45
  • This is my first project where I use classes. I think ft.UserControl is the class that is above both classes. But I have not programmed an own controller class. How to do it? – klettermax Apr 10 '23 at 18:51
  • No, somewhere you have a "main window" class that is creating a `UserID` object and a `RegisterUser` object. Right? That's the guy that has to connect the two. – Tim Roberts Apr 10 '23 at 20:25
  • yes, I have that and there the two are also connected:`import flet as ft from pages.welcome import Welcome as v1 from pages.register_sample import SampleID as v2 from pages.empty import Empty as v3 from pages.select_workflow import SelectWorkflow as v4 from modules.register_user import RegisterUser as v5 def main(page: ft.Page): page.title = "Workflow" page.theme_mode = "light" page.window_height = 950 page.window_width = 1300 welcome = v1() ... register_user = v5(user_id=UserID) def route_change(route):.... ` – klettermax Apr 10 '23 at 20:30
  • Sorry about the bad formatting. I hope you can read it anyway. – klettermax Apr 10 '23 at 20:51
  • That's not right. You are passing the `UserID` class to `RegisterUser`, not an INSTANCE of that class. You don't ever create that control, unless there's code you haven't shown us. You would need `self.user_id = user_id()` to do that. Once you have that, you can override its method, if you need to. – Tim Roberts Apr 10 '23 at 22:27
  • i need to do this in main because i have self.user_id() = user_id() in the __init__ of RegisterUser (see it in the first post). main is not a class. I don't understand how I can implement it differently. – klettermax Apr 11 '23 at 06:08
  • You do NOT have `self.user_id() = user_id()`. You have `self.user_id = user_id`. Neither of those is correct, but the difference between them is critical. I'm not suggesting you change `main`. I'm suggesting you change `RegisterUser`, since it is being handed the class it has to create. – Tim Roberts Apr 11 '23 at 06:50
  • I am really sorry. I don't think I understand you correctly. When I change self.user_id = user_id to self.user_id() = user_id() I only get errors. You also write that both are wrong. What is the correct way to do it? – klettermax Apr 11 '23 at 09:20
  • I have deposited the entire code on GitHub. If you want, I can share it with you, maybe it will be helpful to solve the problem. – klettermax Apr 11 '23 at 09:43
  • I showed you 3 messages ago exactly what to type. Do you understand what that code is trying to do? You need to create a `UserID` object and store it in a member variable. You create the object by doing `user_id()`. You store it in a member variable by `self.user_id =`. – Tim Roberts Apr 11 '23 at 17:33
  • In my opinion, you should find some local help. You need someone to sit down with you and debug this, to trace through the flow. We can't do that remotely. – Tim Roberts Apr 11 '23 at 17:38

0 Answers0