Results 1 to 4 of 4

Thread: Advice on UI python code integration

  1. #1
    Join Date
    Jul 2017
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Advice on UI python code integration

    I have a functioning python script I'm wanting to integrate into a UI I created using pyqt5. I'm having trouble with the radio buttons.

    I've attached a pic of the UI. The Idea is the user inputs a value into the top text box, then click the radio button indicating which way they want the conversion to be performed then click the action button to perform the calculation.

    here is a sample of the working python code file that does the conversion from IDLE.

    I've also attached my converted UI file as well.

    I know I'll need to check the radio buttons would it be best to do so in a loop?

    Qt Code:
    1. def Fluid_converter():
    2.  
    3. while True:
    4. P1 = input("Convert to ML,OZ (select one), 'Q'=quit: ")
    5. print (P1)
    6.  
    7.  
    8. if P1.lower() == "ml":
    9. Volume = float(input("What is your Volume?"))
    10. answer = (Volume / 29.5735296)
    11.  
    12. print(round(answer, 4))
    13. elif P1.lower() == "oz":
    14. Volume = float(input("What is your Volume?"))
    15. answer = (Volume * 29.5735296)
    16. print(round(
    To copy to clipboard, switch view to plain text mode 




    ConverterUIpic.jpgfuchsuiv2.pyConverterUIpic.jpg

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Advice on UI python code integration

    I know I'll need to check the radio buttons would it be best to do so in a loop?
    No. You need to read about the Qt Signals and Slots architecture. You also need to understand how to convert a program from command line driven (as your script is) to one that is event driven (as Qt GUI apps are).

    In an event-driven program, Qt's "event loop" takes the place of your "while True:" loop. You will write slots and connect them to the ML and OZ radio buttons' "clicked" or "toggled" signals. In those slots, you will set a member variable to true if the conversion is ml -> oz or false if it is the other way around (for example). You may also have slots that are connected to the input line edit's "editingFinished" signal, to the calculate button's "clicked" signal, and so forth.

    Once your UI is set up and the event loop is running, your own code does absolutely nothing until something the user does with your UI results in one of your program's slots being called.

    I suggest you look at some of the many, many example programs in your Qt distribution. You can also look at some of the good tutorial sites, like K Hong's BogoToBogo. The Python.org Wiki has plenty of PyQt examples and tutorials as well.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jul 2017
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Advice on UI python code integration

    Thanks for getting me started but I'm still having an issue understanding the signals and slots use with a radio button. I'm not understanding how we can read a radio button as a signal.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Advice on UI python code integration

    I'm not understanding how we can read a radio button as a signal.
    As I said, you need to understand event-driven programming. In event-driven programming, your code is not in charge, and it usually does not "read" anything. Instead, your program contains code that reacts to things happening in the user interface. When something you are interested in happens, the code you have written to react to it gets executed.

    Your code does not sit there spinning its wheels and continually asking the UI if anything has changed. Your code does nothing until the UI sends it a signal (by calling one of your functions) to let you know something has changed. Once your code is done reacting to that change, it goes back to doing nothing until the next UI signal or event comes along. (Behind the scenes, the Qt event loop is running and looking for things like key clicks and mouse actions, but your program does not need to be aware of or care about that. It just waits for its slots to get executed).

    You do not "read" radio buttons in this scenario. A QRadioButton (actually its base class, QAbstractButton) has a signal (member function) named "clicked()". The signal also passes a Boolean parameter that is "true" if the radio button is checked, false if it is not. When the user clicks the radio button, it executes the clicked() function. That function determines if there are any slots connected to it, and if so, calls those slots (which are just member functions as well) and passes the Boolean "checked" value.

    In your dialog / main window class, you define slots, call them whatever you want, that you will connect to the two radio buttons' signals. Say you call these slots "onMLClicked( bool bChecked )" and "onOZClicked( bool bChecked )". You also create a member variable for your class, a Boolean named "mlToOz" for example. In your slots, you examine the value of bChecked. In "onMLClicked()" if bChecked is true, then you set mlToOz to true also because that's the radio button that is turned on. If bChecked is false, you set mlToOz to false. In the other slot (onOzClicked()), you do exactly the opposite - set the variable to false if bChecked is true and vice-versa, because if OZ is checked, then you want to do the opposite conversion.

    To wire things up, when you create your radio buttons, you call QObject::connect() to connect the radio button signal to the corresponding slot in your class. In C++ this looks like the following; you'll have to learn the corresponding Python syntax:

    Qt Code:
    1. // Usually this code goes in the MyDialog constructor; in Python, that's the _init_ method, I think
    2.  
    3. connect( mlButton, &QRadioButton:clicked, this, &MyDialog::onMLClicked );
    4.  
    5. // And say you also have a Calculate button, you connect its signal to your slot
    6.  
    7. connect( calculateBtn, &QPushButton::clicked, myDialog, &MyDialog::onCalculate );
    8.  
    9. // And the slots look like this
    10.  
    11. void MyDialog::onMLClicked( bool bChecked )
    12. {
    13. if ( bChecked )
    14. mlToOz = true;
    15. else
    16. mlToOz = false;
    17. }
    18.  
    19. void MyDialog::onCalculate()
    20. {
    21. if ( mlToOz )
    22. // perform the ML -> OZ conversion of the value in the input line edit
    23. else
    24. // perform the OZ -> ML conversion
    25.  
    26. // and set the result (after formatting to a string) into the output line edit
    27. }
    To copy to clipboard, switch view to plain text mode 

    Why don't you study this tutorial? It is not so much different from what you want to do.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Rewrite code from C++ to Python
    By Pedro Monteiro in forum Qt Programming
    Replies: 0
    Last Post: 6th January 2016, 21:52
  2. PyQt4 GUI to python code Help
    By dan_cuspi in forum Newbie
    Replies: 0
    Last Post: 26th November 2015, 19:47
  3. convert c++ class to python code
    By alrawab in forum Qt Programming
    Replies: 8
    Last Post: 25th June 2012, 09:43
  4. execute python in the Qt code
    By gorka_sm in forum Newbie
    Replies: 0
    Last Post: 28th April 2010, 16:04
  5. Embedding python code in Qt
    By lixo1 in forum Qt Programming
    Replies: 2
    Last Post: 12th March 2010, 19:02

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.