Changing button text with popup text input kivy - python-3.x

So, I'm new to kivy and can't get this code to work. I'm trying to update a button text with a popup text input. The popup shows when the button is pressed, and when it's dismissed it should update the text on the button with whatever text was typed in it.
I've tried many variations of this code, but no one have worked. Either nothing happens or I get this error:
AttributeError: 'super' object has no attribute '__getattr__'
Here it is:
main.py
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.popup import Popup
class MainWidget(GridLayout):
pass
class PopText(Popup):
def textChange(self):
MyButton().change()
def getText(self):
text = self.display
return text
class MyButton(AnchorLayout):
def change(self):
self.ids.equip_bt.text = PopText().getText()
print(self.ids.my_bt.text)
class MyApp(App):
def build(self):
return MainWidget()
if __name__ == "__main__":
MyApp().run()
my.kv
#:kivy 1.10.0
#:import F kivy.factory.Factory
<PopText>:
size_hint: .7, .3
title: "Input Text"
on_dismiss: self.textChange()
display: pop_text.text
TextInput:
id: pop_text
focus: True
multiline: False
on_text_validate: root.dismiss()
<MyButton>:
anchor_y: "top"
anchor_x: "right"
Button:
id: my_bt
text: "Input Text"
on_release: F.PopText().open()
<MainWidget>:
cols: 1
rows: 2
MyButton:
MyButton:
Any ideas on how to solve this?

Here is a minimum example of what you are trying to achieve. The hard part is connection the button from the Popup to the Button which opened it. I am going through the app class to achieve that. I got to admit it is not a pretty solution.
from kivy.app import App
from kivy.uix.popup import Popup
from kivy.uix.button import Button
class PopupBttn(Button):
def openPopup(self):
Pop = TextPopup().open(self.text)
class TextPopup(Popup):
def open(self, text, **kwargs):
super(TextPopup, self).open(**kwargs)
self.ids.txtipt.text = text
class MyApp(App):
pass
if __name__ == "__main__":
MyApp().run()
kv file:
BoxLayout:
PopupBttn:
id: bttn
text: 'open Popup'
on_press: self.openPopup()
<TextPopup>:
BoxLayout:
orientation: "vertical"
TextInput:
id: txtipt
Button:
text: "OK"
on_release: app.root.ids.bttn.text=root.ids.txtipt.text
on_release: root.dismiss()
Here is an updated version to use multiple buttons. Unfortunately, you will need to set ids and name to the string of id per Button.
python file
from kivy.app import App
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.properties import StringProperty
class PopupBttn(Button):
def openPopup(self):
print(self.name)
Pop = TextPopup().open(self.text, self.name)
class TextPopup(Popup):
bttnid = StringProperty()
text = StringProperty()
def open(self, text, id, **kwargs):
super(TextPopup, self).open(**kwargs)
self.ids.txtipt.text = text
self.bttnid = id
def setText(self):
App.get_running_app().root.ids[self.bttnid].text = self.text
class MyApp(App):
pass
if __name__ == "__main__":
MyApp().run()
kv file
BoxLayout:
orientation: 'vertical'
PopupBttn:
name: 'one'
id: one
text: 'I am the first Button'
PopupBttn:
name: 'two'
id: two
PopupBttn:
name: 'three'
id: three
PopupBttn:
name: 'four'
id: four
text: 'I am the fourth button'
<PopupBttn>:
text: 'open Popup'
on_press: self.openPopup()
<TextPopup>:
text: txtipt.text
BoxLayout:
orientation: "vertical"
TextInput:
id: txtipt
Button:
text: "OK"
on_release: root.setText()
on_release: root.dismiss()

In order to update both buttons, you need to assign unique id to each of them. Please refer to the example below for details.
Example
main.py
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.popup import Popup
from kivy.properties import ObjectProperty
class MainWidget(GridLayout):
btn_top = ObjectProperty()
btn_bottom = ObjectProperty()
class PopText(Popup):
pass
class MyButton(AnchorLayout):
my_btn = ObjectProperty()
class TestApp(App):
title = "Changing button text with popup text input Kivy"
def build(self):
return MainWidget()
if __name__ == "__main__":
TestApp().run()
test.kv
#:kivy 1.10.0
#:import F kivy.factory.Factory
<PopText>:
size_hint: .7, .3
title: "Input Text"
TextInput:
focus: True
multiline: False
on_text_validate:
app.root.btn_top.my_btn.text = self.text
app.root.btn_bottom.my_btn.text = self.text
root.dismiss()
<MyButton>:
my_btn: my_bt
anchor_y: "top"
anchor_x: "right"
Button:
id: my_bt
text: "Input Text"
on_release: F.PopText().open()
<MainWidget>:
btn_top: btn_top
btn_bottom: btn_bottom
cols: 1
rows: 2
MyButton:
id: btn_top
MyButton:
id: btn_bottom
Output

Related

Print button dynamic size in console - Kivy

I would like to print button size in console. Able to get root window size, any idea how to get the buttons?
Hoping some way iterate through the widget ids?
.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class TestOut(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def on_size(self, *args):
# print(str(self.size))
pass
def get_size(self):
for key, val in self.ids.items():
print(key, val)
# print(self.ids.button1.size)
class PlaygroundApp(App):
title = "test"
def build(self):
return TestOut()
if __name__ == "__main__":
PlaygroundApp().run()
.kv
<TestOut>:
Button:
id: button1
text: 'A'
width: dp(100)
size_hint: None, 1
Button:
id: button2
text: 'B'
Thanks in advance!
<TestOut>:
Button:
id: button1
text: 'A'
width: dp(100)
size_hint: None, 1
on_release:
print(button1.size)
Button:
id: button2
text: 'B'
on_release:
print(button2.size)

python kivy, why my "enter" button with "on_keyboard_down" not working

I am working on the same issue from my previous post. Now I added screen manager, and I can switch to different pages.
why is my kivy program not calling the function from another class?
I am still confused about OOP with GUIs, however I tried the following, and none of them worked.
I tried to add an instance of Screen 1() in the main app(), and it did nothing. Then, I tried to add the contractor method init into Screen_1() and it says I don't have a build method. And few other ideas that didn't work at all.
Then I realized that there is no link between Screen_Manager() and Screen_1() because all of my methods are in Screen_1(), but the build method is returning Screen_Manager(). On my kv file, there is this code:
<Screen_Manager>:
Screen_1:
Screen_2:
isn't this is where the program "links" between the Screen_Manager() class to other classes?
if someone can help me understand what I am not understanding and help me correct my problem, it would help me to learn about kivy. I understand, <> is like apply to rules, which has 2 widgets screen 1 and screen 2, and also have their own rules.
here is my main.py
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.lang.builder import Builder
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.modules import keybinding
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen
class Screen_Manager(ScreenManager):
pass
class Screen_1(Screen):
main_display = ObjectProperty()
text_input = ObjectProperty()
def plus_1(self):
self.value = int(self.main_display.text)
self.main_display.text = str(self.value + 1)
def minus_1(self):
self.value = int(self.main_display.text)
self.main_display.text = str(self.value - 1)
def up(self):
self.main_display.text = self.text_input.text
self.text_input.text = ''
class Keyboard(Widget):
def __init__(self, instance):
super().__init__()
self.a = instance
self.keyboard = Window.request_keyboard(None, self)
self.keyboard.bind(on_key_down=self.on_keyboard_down)
def on_keyboard_down(self, keyboard, keycode, text, modifiers):
if keycode[1] == 'enter':
self.a.up()
return True
class Screen_2(Screen):
pass
class MainApp(App):
def build(self):
self.title = "Number Control App"
self.sm = Screen_Manager()
key = Keyboard(self.sm)
return self.sm
if __name__=="__main__":
MainApp().run()
my kv file
<Screen_Manager>:
Screen_1:
Screen_2:
<Screen_1>:
name: "first"
main_display: display_1
text_input: text_input_1
#------inherenting from BoxLayout
BoxLayout:
orientation: "vertical"
#------begining of conent--
BoxLayout:
size_hint_y: 1
#-nexted content------
Label:
id: display_1
text: "0"
font_size: "150sp"
background_color: 0.1, 0.5, 0.6,
#--------
BoxLayout:
size_hint_y: .35
orientation: "horizontal"
#-nexted content------
Button:
text: "-"
font_size : "60sp"
on_press: root.minus_1()
#-nexted content------
Button:
text: "+"
font_size : "35sp"
background_color: (0, 1, 0, 1)
on_press: root.plus_1()
#--------
BoxLayout:
size_hint_y: .15
orientation: "horizontal"
#-nexted content-------
Button:
text: "Config"
size_hint_x: .25
on_release:
root.manager.current = "second"
root.manager.transition.direction = "left"
#-nexted content-------
TextInput:
id: text_input_1
size_hint_x: 1
hint_text: "Enter your initial # here"
multiline: False
#-nexted content-------
Button:
text: "Up"
size_hint_x: .25
on_press: root.up()
<Screen_2>:
name: "second"
Button:
text: "Go Back"
on_press:
app.root.current = "first"
root.manager.transition.direction = "right"enter code here
thanks again for taking the time to help.
Not sure I understand your question, but pressing Enter in your TextInput executed your code:
self.a.up()
but a in your KeyBoard is the Screen_Manager as set at the line:
self.sm = Screen_Manager()
key = Keyboard(self.sm)
and Screen_Manager has no up method. You can fix that by changing the code in your KeyBoard code to:
self.a.current_screen.up()
Note that this will only work when the current screen is Screen_1.

Kivy. Refreshing screen with different data

When I click on the button I need the data from the code to be shown on different screen. Below code does what I need despite that Clock adds widgets every second. I would like the TextInput to be editable so when I type some value to the 'Row' it will stay as typed. I've tried to use Clock.schedule_once() but then the widgets does not appear. I've even tried something like below:
clock = 1
def fill_with_data(self, dt)
if clock == 1:
for item ...
clock +=1
witch locks adding new widgets (ridicules I know) but when I go back to MainScreen to show another data then it does not appear on the screen. Below you will find whole code.
from kivy.config import Config
Config.set('graphics', 'multisamples', '0')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import Screen
from kivy.clock import Clock
kv = """
#:import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManager:
MainScreen:
ShowData:
<DataSwitch>
Button:
text: 'Data 1'
on_press: app.root.current = 'ShowData'
on_press: root.show_data1()
Button:
text: 'Data 2'
on_press: app.root.current = 'ShowData'
on_press: root.show_data2()
<Row>
TextInput:
id: text_input
size_hint_y: None
height: 30
<Rows>
orientation: 'vertical'
<MainScreen>
name: 'MainScreen'
DataSwitch:
<ShowData>:
name: 'ShowData'
Rows:
Button:
text: 'Go back'
size_hint_y: None
height: 20
on_press: app.root.current = 'MainScreen'
"""
class Row(BoxLayout):
text = ''
def __init__(self, **kwargs):
super(Row, self).__init__(**kwargs)
self.set_text()
def set_text(self):
print('set', self.text)
self.ids.text_input.text = self.text
class Rows(BoxLayout):
data =[]
def __init__(self, **kwargs):
super(Rows, self).__init__(**kwargs)
#self.fill_with_data()
Clock.schedule_interval(self.fill_with_data, 1)
#Clock.schedule_once(self.fill_with_data)
def fill_with_data(self, dt):
for item in self.data:
Row.text = item
row = Row()
self.add_widget(row)
#self._rows[str(self.row_id)] = weakref.ref(row)
class MainScreen(Screen):
pass
class DataSwitch(BoxLayout):
data1 = ['1', '2', '3', '4']
data2 = ['5', '6', '7', '8']
def __init__(self, **kwargs):
super(DataSwitch, self).__init__(**kwargs)
def show_data1(self):
print('data1', self.data1)
Rows.data = self.data1
Rows()
def show_data2(self):
Rows.data = self.data2
Rows()
class ShowData(Screen):
pass
sm = Builder.load_string(kv)
class TestApp(App):
def build(self):
return sm
if __name__ == '__main__':
TestApp().run()
Running the app will produce the following:
Problem 1 - too many Rows instantiated
Rows: as per kv file
Rows() as per Python script in methods show_data1() and show_data2(). These Rows instances created does not have an associated ModalView.
Problem 2 - switched screen before populating list, data
Button:
text: 'Data 1'
on_press: app.root.current = 'ShowData'
on_press: root.show_data1()
Button:
text: 'Data 2'
on_press: app.root.current = 'ShowData'
on_press: root.show_data2()
Solution
The following example is just an illustration.
kv file - Add id: rows
Add id: rows to Rows: in class rule, <ShowData>:. This will be use to reference attributes or methods in class Rows().
kv file - invoke fill_with_data()
Use on_pre_enter event of Screen and Clock.schedule_once() function to invoke method fill_with_data()
Note:
Row will be added each time, the screen is displayed. In other words, Row widget will double each time. To prevent this, one might want to delete the widgets added in on_pre_leave event.
Snippets - kv file
<ShowData>:
name: 'ShowData'
on_pre_enter:
Clock.schedule_once(self.ids.rows.fill_with_data, 0.1)
Rows:
id: rows
py file - Access attributes / methods in class Rows()
Get object, app using App.get_running_app()
Get object, ShowData using Screen Manager's get_screen() function e.g. root.get_screen('ShowData')
Access attribute, data using ids.rows.data
Snippets
def show_data1(self):
App.get_running_app().root.get_screen('ShowData').ids.rows.data = self.data1
def show_data2(self):
App.get_running_app().root.get_screen('ShowData').ids.rows.data = self.data2
Example
main.py
from kivy.config import Config
Config.set('graphics', 'multisamples', '0')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import Screen
kv = """
#:import FadeTransition kivy.uix.screenmanager.FadeTransition
#:import Clock kivy.clock.Clock
ScreenManager:
MainScreen:
ShowData:
<DataSwitch>
Button:
text: 'Data 1'
on_press: root.show_data1()
on_press: app.root.current = 'ShowData'
Button:
text: 'Data 2'
on_press: root.show_data2()
on_press: app.root.current = 'ShowData'
<Row>:
TextInput:
id: text_input
size_hint_y: None
height: 30
<Rows>:
orientation: 'vertical'
<MainScreen>:
name: 'MainScreen'
DataSwitch:
<ShowData>:
name: 'ShowData'
on_pre_enter:
Clock.schedule_once(self.ids.rows.fill_with_data, 0.1)
Rows:
id: rows
Button:
text: 'Go back'
size_hint_y: None
height: 20
on_press: app.root.current = 'MainScreen'
"""
class Row(BoxLayout):
def __init__(self, text, **kwargs):
super(Row, self).__init__(**kwargs)
self.ids.text_input.text = text
class Rows(BoxLayout):
data = []
def fill_with_data(self, dt):
for item in self.data:
row = Row(text=str(item))
self.add_widget(row)
class MainScreen(Screen):
pass
class DataSwitch(BoxLayout):
data1 = ['1', '2', '3', '4']
data2 = ['5', '6', '7', '8']
def show_data1(self):
App.get_running_app().root.get_screen('ShowData').ids.rows.data = self.data1
def show_data2(self):
App.get_running_app().root.get_screen('ShowData').ids.rows.data = self.data2
class ShowData(Screen):
def on_pre_leave(self):
self.ids.rows.clear_widgets()
sm = Builder.load_string(kv)
class TestApp(App):
def build(self):
return sm
if __name__ == '__main__':
TestApp().run()

How to print value in a TextInput when a Button is pressed? (Kivi)

I'm making a logging GUI but I can't find how to print the values of two TextInputs when a button is pressed.
This is my .py:
import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
#[...]
class Login_Screen(BoxLayout):
def register(self):
print('Hi! I want here have the user and password, so later I can make a proper register function.')
pass
class MainApp(App):
def build(self):
return Login_Screen()
if __name__ == '__main__':
MainApp().run()
This is my .kv: (The important part is in the last line.)
#[...]
<Login_Screen>:
#[...]
BoxLayout:
AnchorLayout:
#[...]
TextInput01: # Come from #TextInput
id: user_input
BoxLayout:
#[...]
AnchorLayout:
TextInput01: # Come from #TextInput
id: password_input
BoxLayout:
Button01: # Come from #Button
id: login
text: 'Login'
Button01: # Come from #Button
id: register
text: 'Register'
on_press: root.register()
#[...] is code that I delete in order to make this more clear.
I want to print password_input value and user_input value when I press register.
The code must be in the .py file because I don't want exactly only print the values, I want to make something more functional but this in an example for me to understand.
Please refer to the snippet and example below.
Snippet
def register(self):
print("user: ", self.ids.user_input.text)
print("password: ", self.ids.password_input.text)
Example
main.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class Login_Screen(BoxLayout):
def register(self):
print("user: ", self.ids.user_input.text)
print("password: ", self.ids.password_input.text)
class MainApp(App):
def build(self):
return Login_Screen()
if __name__ == '__main__':
MainApp().run()
main.kv
#:kivy 1.10.0
<Login_Screen>:
BoxLayout:
AnchorLayout:
TextInput:
id: user_input
BoxLayout:
AnchorLayout:
TextInput:
id: password_input
BoxLayout:
Button:
id: login
text: 'Login'
Button:
id: register
text: 'Register'
on_press: root.register()
Output

Changing Kivy Spinner values dynamically

I´m looking for a way to set the values of a Kivy Spinner depending on the text chosen from the parent spinner. The problem is that I have defined both spinners in kv language. I´m not sure if the approach would be to destroy the second spinner each time a new value is chosen from the first one, then regenerate it (if so I have no idea how to do that because of the kv code), or if there would be a way to dynamically update the "values" of the second spinner based on an on_text procedure updating the list.
In all cases the below code does not work. Any help appreciated. Thanks.
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.spinner import Spinner
Builder.load_string('''
<MainScreen>:
AnchorLayout:
anchor_x: 'center'
anchor_y: 'top'
BoxLayout:
size_hint: 1, .9
orientation: 'vertical'
padding: 80
spacing: 20
Spinner:
id: spinner_1
text: '< Select >'
values: root.pickType
on_text: root.updateSubSpinner(spinner_1.text)
Spinner:
id: spinner_2
text: '< Select >'
values: root.pickSubType
AnchorLayout:
anchor_x: 'center'
anchor_y: 'bottom'
size_hint: 1, .1
Button:
on_press: root.onExit()
text: 'Exit'
font_size: 50
''')
class MainScreen(FloatLayout):
def __init__(self, **kwargs):
self.buildLists()
super(MainScreen, self).__init__(**kwargs)
def buildLists(self):
self.pickType = ['Select','#1','#2','#3']
self.pickSubType = ['Select']
def updateSubSpinner(self,text):
if text == '#1':
self.pickSubType.extend('A'+'B')
elif text == '#2':
self.pickSubType.extend('P'+'Q')
else:
self.pickSubType.extend('Y'+'Z')
def onExit(self):
App.get_running_app().stop()
class TestApp(App):
def build(self):
return MainScreen()
if __name__ == "__main__":
TestApp().run()
You should reference the child spinner values list, not the pickSubType list, which isn't even bound to it. Example:
def updateSubSpinner(self, text):
self.ids.spinner_2.text = 'Select'
if text == '#1':
self.ids.spinner_2.values = ['A', 'B']
elif text == '#2':
self.ids.spinner_2.values = ['P', 'Q']
else:
self.ids.spinner_2.values = ['Y', 'Z']
Writing values: root.pickSubType doesn't bind the pickSubType to the values, if the first isn't a ListProperty. You could upgrade it to a ListProperty if you like, but it is not necessary.

Resources