You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python类C1与C2之间传递text变量?

How to Pass the text Variable from Class C1 to Class C2

Hey there! Let's work through how to get that text value from your C1 class over to C2's f3 method. Based on your description, it sounds like you're working with UI classes (probably Qt, given the setupUi mention), so I'll tailor examples to that context—but the core ideas apply to any Python classes.

First, let's recap your class structure to align on the starting point:

# Assume parent class is a Qt widget like QWidget
class C1(QWidget):
    def __init__(self, x1, f1):
        super().__init__()
        self.setupUi(self)
        self.text = self.FMB.text()  # Grab text from your UI element

class C2(QWidget):
    def __init__(self, x2, f2):
        super().__init__()
        self.setupUi(self)
    
    def f3(self, t):
        # Your logic to process the received text
        print(f"Got text from C1: {t}")

Common Pitfalls You Might Be Hitting

  • Trying to access C1.text directly as a class variable (but text is an instance variable, so you need a specific C1 object to pull it from)
  • Calling f3 before C1 has finished initializing its text value
  • Not passing the C1 instance or its text value to C2 correctly

The cleanest approach is to create instances of both classes in your main code, then explicitly pass the text value from C1 to C2's f3 method. This keeps your classes independent and easy to maintain.

if __name__ == "__main__":
    app = QApplication([])  # Skip this line if not using Qt
    
    # Create instances of both classes
    c1 = C1(x1_value, f1_value)
    c2 = C2(x2_value, f2_value)
    
    # Pass C1's text to C2's f3 method
    c2.f3(c1.text)
    
    # Show your UI windows if needed
    c1.show()
    c2.show()
    
    app.exec_()  # Skip this line if not using Qt

Solution 2: Instantiate C2 Inside C1 and Pass Text Directly

If C2 is always dependent on C1, you can create the C2 instance within C1's __init__ method and pass the text right away:

class C1(QWidget):
    def __init__(self, x1, f1):
        super().__init__()
        self.setupUi(self)
        self.text = self.FMB.text()
        
        # Create C2 instance and pass the text immediately
        self.c2 = C2(x2_value, f2_value)
        self.c2.f3(self.text)
        self.c2.show()  # Show C2's window if required

class C2(QWidget):
    def __init__(self, x2, f2):
        super().__init__()
        self.setupUi(self)
    
    def f3(self, t):
        print(f"Received text: {t}")
        # Update C2's UI with the text if needed, e.g.:
        # self.result_label.setText(t)

Solution 3: Dynamic Updates with Signals & Slots (For Qt UI)

If you need C2 to receive updates whenever C1's text changes (like when the user edits the input field), use Qt's signal-slot system to keep the two classes in sync in real time:

from PyQt5.QtCore import pyqtSignal

class C1(QWidget):
    # Define a signal that emits the updated text
    text_updated = pyqtSignal(str)
    
    def __init__(self, x1, f1):
        super().__init__()
        self.setupUi(self)
        self.text = self.FMB.text()
        
        # Connect the UI element's text change event to our signal
        self.FMB.textChanged.connect(self._on_text_change)
    
    def _on_text_change(self, new_text):
        self.text = new_text
        # Emit the signal with the updated text
        self.text_updated.emit(new_text)

class C2(QWidget):
    def __init__(self, x2, f2):
        super().__init__()
        self.setupUi(self)
    
    def f3(self, t):
        print(f"Updated text from C1: {t}")
        # Update C2's UI in real-time
        self.live_display_label.setText(t)

# Main code to connect everything
if __name__ == "__main__":
    app = QApplication([])
    
    c1 = C1(x1_value, f1_value)
    c2 = C2(x2_value, f2_value)
    
    # Connect C1's signal directly to C2's f3 method
    c1.text_updated.connect(c2.f3)
    
    c1.show()
    c2.show()
    app.exec_()

Key Takeaways

  • Always access text from a C1 instance, not the C1 class itself
  • Ensure C1 has finished initializing self.text before trying to pass it to C2
  • Use signals/slots if you need real-time updates between UI components

内容的提问来源于stack exchange,提问作者Patrick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:13:25