Kivy - changing button color on press - python

I've recently started using kivy to design GUI for my python app. In the app one of the actions is sending requests to server using provided API. After a couple of tests I've noticed one annoying thing that happens only when I want to make a request - the button doesn't change color on press (I'm talking about default action). However, it is changing when simple print() is used.
How I can fix it? Any idea?
This is the code of my test app:
class TestApp(App):
def build(self):
parent = Widget()
btn = Button(text='Add')
btn.bind(on_press=self.add)
parent.add_widget(btn)
return parent
def add(self, obj):
print("Adding")
request = Request(url, urlencode(post_fields).encode())
urlopen(request)

That happened most likely because the UI froze. Theself.add is called, but right after that the UI waits until the request is done, which for you might result in that.
Try to do it like this:
import threading
class TestApp(App):
def build(self):
parent = Widget()
btn = Button(text='Add')
btn.bind(on_press=self.add)
parent.add_widget(btn)
return parent
def add(self, obj):
print("Adding")
#self.printer() # freezing
threading.Thread(target=self.printer).start()
def printer(self, *args):
while True:
print 'blob'
TestApp().run()
Also, instead of on_press use rather on_release. Prevents accidental events if I remember correctly (checks for collision touch↔button area).

Related

How Refresh/Re-initialize Widgets in QStackedWidget

I'm trying to create a script that makes API calls to a website to get info (via the requests module).
The user can then manipulate the website using my script/gui.
My app's main window will have several buttons at the bottom which acts like tabs to switch between windows(let's say for argument's sake 2 buttons to switch between two windows).
When certain changes are made, I need the QStackedWidget to refresh all the widgets/labels in certain windows that are not currently being displayed.
Summary of my code:
# Global Variables
app = QApplication(sys.argv)
win = QtWidgets.QStackedWidget()
response = {} # global dict to store API response from website
class UiWindowOne(QMainWindow):
# This window mostly shows information that I get from the website.
def __init__(self):
super(UiWindowOne, self).__init__()
self.setup_ui(self)
self.retranslate_ui(self)
# Then I map buttons to methods
def setup_ui(self, WindowOne):
# This was generated by QT Designer and places widgets
def retranslate_ui(self, WindowOne):
# This was generated by QT Designer and places widgets
def refresh(self):
'''
This function refreshes the current window. Basically, I put everything in the __init__ function in here (except "super(UiWindowOne, self).__init__()".
:return: None
'''
self.setup_ui(self)
self.retranslate_ui(self)
# Also map buttons to methods
class UiWindowTwo(QMainWindow):
def __init__(self):
super(UiWindowTwo, self).__init__()
self.setup_ui(self)
self.retranslate_ui(self)
# Then I map buttons to methods
def setup_ui(self, WindowTwo):
# This was generated by QT Designer
def retranslate_ui(self, WindowTwo):
# This was generated by QT Designer
def refresh(self):
'''
This function refreshes the current window. Basically, I put everything in the __init__ function in here (except "super(UiWindowTwo, self).__init__()".
:return: None
'''
self.setup_ui(self)
self.retranslate_ui(self)
# Also map buttons to methods
def update_website(self):
# Make changes to website
# After changes were made, I want to get modified info from the website and re-initialize/refresh both windows to reflect the changes made.
# I can easily call self.refresh() to refresh WindowTwo. But I cannot refresh WindowOne from here.
def main():
# Here I make API calls to the Website to get info/images
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(".\\imgs/static/A.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
win.setWindowIcon(icon)
win.setWindowTitle("NAME")
first_window = UiWindowOne()
second_window = UiWindowTwo()
win.addWidget(first_window)
win.addWidget(second_window)
win.setGeometry(250, 250, 820, 854)
win.setFixedWidth(820)
win.setFixedHeight(854)
win.show()
sys.exit(app.exec())
if __name__ == '__main__':
main()
I have tried doing a "first_window.refresh()" under the update_website() function in UiWindowTwo, but then python tells me that first_window is not defined.
I then tried making first_window and second_window global variables, but then I ended up reordering my whole script and couldn't get it to run.

When changing screens with QStackedWidget, how to call a function in the changed screen?

I'm trying to call the init function of the screen I'm changing my screen index to
For an example, i have this code:
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtGui as qtg
from sys import argv as sysArgv
from sys import exit as sysExit
arialLarge = qtg.QFont("Arial", 18)
class MainWindow(qtw.QWidget):
def __init__(self):
super().__init__()
# Current screen label;
mainWindowLabel = qtw.QLabel("This is the main window", self)
mainWindowLabel.setFont(arialLarge)
mainWindowLabel.move(20, 40)
# Button for going to the HelloWindow screen;
gotoHelloWindowButton = qtw.QPushButton("Go to hello window", self, clicked=lambda: appStack.setCurrentIndex(appStack.currentIndex()+1))
gotoHelloWindowButton.move(100, 100)
class HelloWindow(qtw.QWidget):
def __init__(self):
super().__init__()
# EG: print hello world when I visit this page
print("hello world")
# Current screen label;
helloWindowLabel = qtw.QLabel("This is the hello window", self)
helloWindowLabel.setFont(arialLarge)
helloWindowLabel.move(20, 40)
# Button for going to the MainWindow screen;
gotoMainWindowButton = qtw.QPushButton("Go to main window", self, clicked=lambda: appStack.setCurrentIndex(appStack.currentIndex()-1))
gotoMainWindowButton.move(100, 100)
if __name__ == "__main__":
app = qtw.QApplication(sysArgv)
appStack = qtw.QStackedWidget()
appStack.addWidget(MainWindow())
appStack.setFixedSize(300, 300)
appStack.show()
appStack.addWidget(HelloWindow())
sysExit(app.exec())
If im visiting the HelloWindow from the MainWindow, how can i run the init function of the HelloWindow screen so I can run whatever code I want in there?
I need to be able to do this as on the app im working on as on the mainpage i have dynamically created buttons that all have functions parameters with different indexes to my server, and i need to be able to fetch the data from server based off the clicked button's data index so on the other page I can view the desired data.
The __init__ of a python class is what is called when an instance is created (using SomeClass()), so you should not try (or even think) to call it again, as it could create serious problems and bugs that are hard to track.
I strongly suggest you to read the documentation about classes in Python, as you cannot ignore that aspect in object oriented programming.
If you need to call something everytime the index is changed, then you should better subclass QStackedWidget and control everything from there.
A good solution is to create a standardized function that will be called everytime the page is presented, and ensure that the stack widget correctly calls it.
class FirstPage(QtWidgets.QWidget):
def __init__(self):
super().__init__(self)
# ...
self.nextButton = QtWidgets.QPushButton('Next')
self.doSomething()
def doSomething(self):
...
class SecondPage(QtWidgets.QWidget):
def __init__(self):
super().__init__(self)
# ...
self.prevButton = QtWidgets.QPushButton('Previous')
self.doSomething()
def doSomething(self):
...
class Stack(QtWidgets.QStackedWidget):
def __init__(self):
super().__init__(self)
self.first = FirstPage()
self.first.nextButton.clicked.connect(self.goNext)
self.addWidget(self.first)
self.second = SecondPage()
self.second.prevButton.clicked.connect(self.goPrev)
self.currentChanged.connect(self.initCurrent)
def goNext(self):
self.setCurrentIndex(1)
def goPrev(self):
self.setCurrentIndex(0)
def initCurrent()
if self.currentWidget():
self.currentWidget().doSomething()
if __name__ == "__main__":
app = qtw.QApplication(sysArgv)
appStack = Stack()
appStack.setFixedSize(300, 300)
appStack.show()
sysExit(app.exec())
Note that adding a QMainWindow to a parent is not a good idea, as Qt main windows are intended to be used as top level windows; also note that using fixed geometries (positions and sizes) is often considered bad practice, and you should use layout managers instead.

PyQt5 loading spinner halts on post request

I'm using the QtWaitingSpinner found here: https://github.com/snowwlex/QtWaitingSpinner. You can create and start a spinner like so: spinner = QtWaitingSpinner(self); spinner.start(). Unfortunately when I try to make a POST request from my GUI, the spinner halts until a response has been returned. Consequently I don't see the spinner at all, or if I start the spinner prematurely it stops spinning while it waits for the response. I think I'll have to use some sort of asynchronous method like QThread or asyncio but it's unclear what the best way of getting around this is. If anyone can show me the best way to handle it I'd be grateful. Here is a simplified version of what I'm doing:
class Obj(QDialog):
# some button calls this function when pressed
def submit(self):
#start spinner
spinner = QtWaitingSpinner(self)
spinner.start()
# post some data to some url, spinner should spin
r = requests.post('some_url.com', json=some_data)
# stop spinner
spinner.stop()
The problem you are requests is blocking the Qt loop, so elements like QTimer do not work. One solution is to run that task on another thread, a simple way to do it is using QRunnable and QThreadPool.
class RequestRunnable(QRunnable):
def __init__(self, url, json, dialog):
QRunnable.__init__(self)
self.mUrl = url
self.mJson = json
self.w = dialog
def run(self):
r = requests.post(self.mUrl, json=self.mJson)
QMetaObject.invokeMethod(self.w, "setData",
Qt.QueuedConnection,
Q_ARG(str, r.text))
class Dialog(QDialog):
def __init__(self, *args, **kwargs):
QDialog.__init__(self, *args, **kwargs)
self.setLayout(QVBoxLayout())
btn = QPushButton("Submit", self)
btn.clicked.connect(self.submit)
self.spinner = QtWaitingSpinner(self)
self.layout().addWidget(btn)
self.layout().addWidget(self.spinner)
def submit(self):
self.spinner.start()
runnable = RequestRunnable("https://api.github.com/some/endpoint",
{'some': 'data'},
self)
QThreadPool.globalInstance().start(runnable)
#pyqtSlot(str)
def setData(self, data):
print(data)
self.spinner.stop()
self.adjustSize()
A complete example can be found in the following link

Kivy app exiting on screen change

I'm trying to port my code from the .kv file all to Python (it seems easier for me that way). I've got two screens and I've been switching to the next one using root.manager.current = "main" in the kv file. However, when writing this bit in Python code, I've run into the crash of my app. This is the code I have:
class CustomScreenManager(ScreenManager):
def switch_view(self):
self.current = 'main'
class Intro(Screen):
pass
class Chat(Screen):
pass
class ChatApp(App):
def build(self):
Screens = CustomScreenManager(transition = NoTransition())
intro = Intro()
chat = Chat(name = "main")
bt1_intro = Button(on_press = Screens.switch_view())
intro.add_widget(bt1_intro)
Screens.add_widget(intro)
Screens.add_widget(chat)
return Screens
if __name__ == "__main__":
ChatApp().run()
ChatApp().screen_manager
I've also tried the switch_to method but it also crashes the app. What can I do to avoid the crashing and get the expected behaviour? Thanks in advance.
Change the definition of switch_view to
def switch_view(self, *args):
and add the Button with
bt1_intro = Button(on_press = Screens.switch_view)
The app crashes because in your original assignment of bt1_intro switch_view gets called (rather than passed to the function), and at the time the screen does not exist.

QDialog switch to non-modal after accept not redrawing

So, I'm not sure if the title is the best description, but it's what I came up with.
Here's the deal. I'm working on a PyQt app that has a sort of plugin system where you can just add some sub classes to a folder and the app finds them. These commands have the option of being able to create little uis for themselves. Basically, they look like this:
class Command(object):
def do(self):
self.setupUi()
self.pre()
self.run()
self.post()
def pre(self):
# do setup stuff for run method
def run(self):
# do actual work
def post(self):
# clean up after run
def setupUi(self):
# create a ui for this command
diag = QDialog()
diag.exec_()
Now, the issue I'm running into is, I have one Command that creates a dialog, and waits for the user to accept it. Then, I need to switch the dialog to non-modal while the command is running, and up date the dialog. This all seems to work fine. But, the problem is I can't get the dialog to redraw until after the pre, run, and post methods have finished. So, if I have the setupUi like this:
def setupUi(self):
# create a ui for this command
diag = QDialog()
if diag.exec_():
diag.setModal(False)
diag.show()
I tried processEvents but that didn't seem to do it. Has anyone else run into this issue, or know of any work arounds?
Thanks
Using diag.exec_() will block until the dialog returns (is closed). So, if you will need to call show() on it's own. There are a few ways to proceed from here.
You can have the dialog accept slot run a reference to the rest of the commands
You can poll the dialog to see if the user has accepted
You can move the pre, run, and post commands to the dialog
Assuming you want to keep the meat of the code out of the dialog class, and since periodically polling is best to avoid if possible, here is an example of the first strategy:
import sys, time
from PyQt4 import QtCore, QtGui
class MyDialog(QtGui.QDialog):
def __init__(self,parent=None):
QtGui.QDialog.__init__(self,parent)
layout = QtGui.QVBoxLayout()
self.msg = QtGui.QLabel('some sort of status')
self.buttonbox = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Ok, QtCore.Qt.Horizontal, self)
self.buttonbox.accepted.connect(self.accept)
layout.addWidget(self.msg)
layout.addWidget(self.buttonbox)
self.setLayout(layout)
def set_msg(self, new_msg):
self.msg.setText(new_msg)
def set_function_on_accept(self,fcn):
self.function = fcn
def accept(self):
self.function()
class Command(object):
def do(self):
self.setupUi()
def do_work(self):
self.pre()
self.run()
self.post()
def pre(self):
# do setup stuff for run method
time.sleep(1)
self.diag.set_msg("stuff setup")
QtGui.QApplication.processEvents()
def run(self):
# do actual work
time.sleep(1)
self.diag.set_msg("work done")
QtGui.QApplication.processEvents()
def post(self):
# clean up after run
time.sleep(1)
self.diag.set_msg("cleaned up")
QtGui.QApplication.processEvents()
def setupUi(self):
# create a ui for this command
diag = MyDialog()
self.diag = diag
diag.set_function_on_accept(self.do_work)
diag.show()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
command = Command()
command.do()
sys.exit(app.exec_())

Categories