Init screen in kv on button release using ScreenManager - python

I'm a beginner Kivy developer and I need some advice from you guys.
I'm using ScreenManager to jump between screens and as far as I noticed, all the screens are initialized just after the application starts, and I need them to be initialized with certain attributes from previous screens, like, selecting the category or stuff. Is there any way to do that?
I have two buttons in CategorySelectScreen both representing certain category, I want them to send a string attribute to DictScreen, where it will be used as an argument in CategorySelect method, which filters the items list, but the thing is, the application need that argument on start and without it the interpreter would just throw errors.
Also, I think I'm using kivy in a very bad way, could you please look into my code and give me some pro tips? Thanks in advance, cheers :)
kv file: http://pastebin.com/UdvGS7Wv
py files: http://pastebin.com/gJn9Mrip

When declaring your screens decide what object would be it's input. Then make this object a property. After that, setup on_... callback where you build your screen with widgets with values based on this input object. For example:
class DictScreen(Screen):
category_selected = ObjectProperty(None)
def on_category_selected(self, instance, value):
category_selected = value
self.clear_widgets()
self.add_widget(Button(text=category_selected.name))
And in previous screen, before you switch to DictScreen get its instance from app.root.ids, then assign category_selected to it and then set new current screen with ScreenManager. This way your DictScreen will be immediately build with choosen category right before you switch to it.
"before you switch to DictScreen get its instance" how this can be done? It's well explained here:
https://kivy.org/docs/api-kivy.uix.widget.html?highlight=widget#kivy.uix.widget.Widget.ids

Related

Python, Kivy - QRScanner and ScreenManager

I want to create an app with 2 (an more) screens (screen manager).
When i make app without that QR reader, i know how to move values from one screen to another. But here i have problem with that.
I spend 3 days on this problem and still dont have answer. Can You help me?
Heres code:
https://github.com/fornakter/Terminarz-Kivy-MD/blob/master/main.py
Errors are on class SecoundWindow, on line 16.
Comments explained errors wit i recive.
Thank You.
The documentation says connect_camera() must be called after on_start(). So change the definition of SecoundWindow to simply:
class SecoundWindow(Screen):
pass
And in your ReadQR App, add the following methods:
def on_start(self):
Clock.schedule_once(self.connect_camera)
def connect_camera(self, dt):
secoundWindow = self.root.get_screen('secound')
secoundWindow.ids.preview.connect_camera(camera_id='front', enable_analyze_pixels=True, default_zoom=0.0)
I needed to add camera_id to avoid SEGFAULT.
Unrelated, but the following lines of your code do nothing and can be deleted:
sm = ScreenManager()
sm.add_widget(FirstWindow(name='first'))
sm.add_widget(SecoundWindow(name='secound'))

Tkinter Class and Button use

I am having trouble finding a way to accomplish what I want using buttons in tkinter. I am programming a multi-frame tkinter app, for myself, and each frame is a class, each class has buttons that I place on the screen in the __init__ method. I am having trouble trying to link the buttons and the command functions together.
Example:
class frameHome:
def __init__(self,parent, controller)
self.frame=tk.Frame(parent)
self.buttonOne=tk.Button(self.frame,text="Click Me") # I want to add the command here
self.buttonOne.pack()
def buttonOneClick():
print("You clicked me")
When I add the command in the tk.Button() call it says buttonOneClick not defined. One video I watched said to add the function at the top of the code. I would like to keep the function as a method of the class for organization, as well as keep init at the top of the class and was wondering if there is a way to create a reference to the function so I can have it as a method after the the init method and be able to call it in the init method, because I would like the init method to create the gui and link all widget commands for that class/frame in it.
Thank you for any time and help that you can offer
in your command did you write the command as command=buttonOneClick()? if that's the case then you should replace it with self.buttonOneClick()
The answer I needed was that I forgot the self param in the buttonOneClick(self, msg) method.

I need a way to pull values from a text input written in .kv string to a function in my python functions

So I've created a GUI for a program I'm writing, and what I'm currently stuck on is the running of the calculations I want it to perform. I've done everything in Python 3.5, with a Kivy GUI build string to create the tabs (using tabbed panel and screen manager). I've got a button at the bottom of the tab to which this question pertains which says "Calculate" and should be taking the values of the text entries and operating them as part of the equation I want them to do.
Is the
.get
function going to be helpful in this case for doing this, or do I want to call more like:
class TabSys(TabbedPanel):
def calculate_psc_clicked(self):
#when they click the button which is id'd as "calculate_psc," this function
#will pull the values and perform the calculations
self.Cp_entry = self.Cp_entry.text
self.P_entry = self.P_entry.text
self.lhv_entry = self.lhv_entry.text
The above code is the TabSys class for my build string in kivy language, after it comes the
class nyce_meemApp(App):
def build(self):
return TabSys()
if __name__ == '__main__':
nyce_meemApp().run()
To actually run all the code. I don't want to inundate the question with code, so I'll leave it there, and I can put up any other pieces of code that anybody wants (if need be I can share the file).
Thanks

How to get the objectName of a modified QSpinBox?

I want to keep track of the user modifying the company's forecast in my custom made app.
I created it using qt designer and PyQt4, and I'm using QSpinBox for the quantities (easy way to control range of values and masking the fields to be number only).
The problem I'm having is when I want to get the QSpinBox that triggered my function.
At this moment it's being triggered using valueChanged.connect but could be using anything else.
I can get the int in the spinbox but not the spinbox's name.
Thanks beforehand for the help!
SOLUTION
The QSpinBox element
self.Item = QtGui.QSpinBox(self.centralwidget)
self.Item.setObjectName(_fromUtf8("ItemName"))
Trigger
self.Item.valueChanged.connect(self.foo)
The function it calls
def foo(self,obj):
sender = MainWindow.sender()
print sender.objectName()
In this case "MainWindow" is my QtCore.QObject
AS the question tagged C++. I am answering in both C++ and pyQt4. I never worked in pyQt4. So please excuse me for syntax.
In the "valueChanged" slot use QObject::sender() to get the spin box which triggered it.
And then call "objectName()".
QObject* obj = sender();
QString objName = obj->objectName();
May be in python:
sender = QtCore.QObject.sender()
str = sender.objectName()

Pygtk client app

I want to create a client frontend in pygtk for my Django project. My general idea is to have one main window, and everytime the user has an action that must change the screen to unload previous widgets and load the new ones. E.g if i have a login page, after user logs in he is presented with a customer screen. I want the new screen to be placed on the same main window, kinda like a page stack, but without the "back" functionality. My first thought was to create a function for every screen, a show_login, a show_customers_screen, etc. Is this a good choice or should i try a better one. And a second question, related to the first. Can i create callbacks inside a function?
e.g
This would be a method of MainWindow
def create_login(self):
....creating widgets here
#UnboundLocalError: local variable 'clear_clb' referenced before assignment
btnlogin.connect('clicked', clear_clb, data=None)
def clear_clb(widget, data=None):
..log in process
I know why i get the error. The thing is that the fields i want this func to clear are local in create_login. Is this the right approach?
Define the clear_clb symbol before referencing it:
def create_login(self):
# create widgets
def clear_clb(widget, data=None):
# log in process
btnlogin.connect('clicked', clear_clb, data=None)
However, the more usual, and in my opinion more readable, way of doing this is to save references to your widgets as attributes of self:
def create_login(self):
# create widgets
self.btnlogin = gtk.Button(...
self.btnlogin.connect('clicked', clear_clb)
def clear_clb(self, widget, data=None):
# log in process

Categories