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 ?
Forum Updated to NodeBB v4.3 + New Features

Why click on button opens new PyQt window ?

Scheduled Pinned Locked Moved Unsolved Qt for Python
5 Posts 3 Posters 810 Views 1 Watching
  • 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.
  • S Offline
    S Offline
    Samuel Bachorik
    wrote on 14 Jan 2021, 20:30 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
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 14 Jan 2021, 20:40 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
      • S Offline
        S Offline
        Samuel Bachorik
        wrote on 15 Jan 2021, 11:15 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
        • S Offline
          S Offline
          Samuel Bachorik
          wrote on 15 Jan 2021, 11:36 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

          J 1 Reply Last reply 15 Jan 2021, 12:09
          0
          • S Samuel Bachorik
            15 Jan 2021, 11:36

            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

            J Offline
            J Offline
            JonB
            wrote on 15 Jan 2021, 12:09 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

            1/5

            14 Jan 2021, 20:30

            • Login

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