Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Why click on button opens new PyQt window ?
QtWS25 Last Chance

Why click on button opens new PyQt window ?

Scheduled Pinned Locked Moved Unsolved Qt for Python
5 Posts 3 Posters 794 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Samuel BachorikS Offline
    Samuel BachorikS Offline
    Samuel Bachorik
    wrote on last edited by
    #1

    Hey guys, i have 3 pyhon files that cooperate and my problem is when i click button that should change text in textbox it also open new window. What is causing it here ? I dont get it

    File test.py

    from PyQt5 import QtWidgets
    import sys
    from PyQt5.QtWidgets import QMainWindow, QApplication, QLineEdit
    
    class Window(QMainWindow):
       def __init__(self):
           super().__init__()
           self.textboxvalue2 = ""
           self.setWindowTitle("Samuel")
           self.setGeometry(200, 200, 259, 258)
    
           self.textbox = QLineEdit(self)
           self.textbox.move(5, 5)
           self.textbox.resize(188, 35)
           self.textbox.setText(str(self.textboxvalue2))
           self.textbox.setPlaceholderText("Enter numbers")
           self.textboxValue = self.textbox.text()
    
           self.b10 = QtWidgets.QPushButton(self)
           self.b10.setText("0")
           self.b10.move(68, 219)
           self.b10.resize(60, 35)
           self.b10.clicked.connect(self.clicked0)
           self.show()
    
       def setExpression(self,expression):
           self.textbox.setText(str(expression))
    
       def clicked0(self):
           import Buttons
           z = Buttons.Button()
           z.kkt()
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        window = Window()
        sys.exit(app.exec())
    

    File Buttons.py

    from kkts import abstract
    class Button(abstract):
        def kkt(self):
            super().buttonSent("0")
    
    

    File kkts.py

    import test
    from abc import ABC, abstractmethod
    x = test.Window()
    class abstract:
        @abstractmethod
        def buttonSent(self,number):
            global x
            x.setExpression(number)
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Why are you doing all this convoluted abstract class ?

      Just do the update in your clicked0 method.

      As for your issue, you are creating two MainWindow. One in your main method and the other in your kkts.py file.

      They are both shown because you call show in your class constructor (which is wrong, it's the code that creates the widget that should decide when to show it).

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • Samuel BachorikS Offline
        Samuel BachorikS Offline
        Samuel Bachorik
        wrote on last edited by
        #3

        Hello @SGaist Thank you for you helpfull reply, i already made changes you mentioned and it is working fine, Thank you a lot!

        1 Reply Last reply
        0
        • Samuel BachorikS Offline
          Samuel BachorikS Offline
          Samuel Bachorik
          wrote on last edited by
          #4

          But if i can have one more question, i removed constructor from main.py and i made it normal function and i call this function in if name == "main": And when i do it by this way, textbox again not changing text.... When i have contructor it is working fine, but when i remove construcot and make it function it is not responding

          JonBJ 1 Reply Last reply
          0
          • Samuel BachorikS Samuel Bachorik

            But if i can have one more question, i removed constructor from main.py and i made it normal function and i call this function in if name == "main": And when i do it by this way, textbox again not changing text.... When i have contructor it is working fine, but when i remove construcot and make it function it is not responding

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @Samuel-Bachorik
            Why not show whatever it is you have now so we can see & understand?

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved