I am trying to update a label in python. It is a simple Guess the num game. Currently it prints the Computer responses in PC but I want it to print those to a label.
here is the main .py file
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
import random
secret=random.randint(1,100)
class Application(BoxLayout):
label1=""
def button_press(self):
global label1
if (int(self.user_guess.text)<secret):
print("Higher")
self.label1="Higher"
elif(int(self.user_guess.text)>secret):
print("Lower")
self.label1="Lower"
elif(int(self.user_guess.text)==secret):
print("You WOn")
self.label1="You WOn"
class WeatherApp(App):
pass
if __name__ == '__main__':
WeatherApp().run()
the .kv file
Application:
<Application>:
size:(480,30)
user_guess:guess
size_hint:(None,None)
text:""
orientation: "vertical"
BoxLayout:
Button:
text:'Play'
on_press:root.button_press()
TextInput:
id:guess
text:''
Label:
text:root.label1
I think you should use
self.label1.text="Higher"
Related
This is a simple example but essentially I need to use Splitter to separate and resize two individual Text input boxes horizontally, however when I'm trying to use the Splitter widget in a Screen its creating a double behind the original content and the Splitter is not functioning properly. Any help will be appreciated. Thank You.
main file
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager , Screen
from kivy.core.window import Window
from kivy.factory import Factory
class TestScreen1(Screen):
pass
class TestScreen2(Screen):
pass
class Manager(ScreenManager):
pass
class MyApp(App):
def build(self):
return Builder.load_file("D:\MainProject\TestFiles\my.kv")
if __name__ == "__main__":
MyApp().run()
KV file
Manager:
TestScreen1:
TestScreen2:
<TestScreen1>:
name:"abc1"
BoxLayout:
orientation: 'horizontal'
Label:
text:"Next"
Splitter:
sizable_from:"left"
Button:
text: "next"
on_press: root.manager.current="abc2"
<TestScreen2>:
name:"abc2"
BoxLayout:
orientation: 'horizontal'
Label:
text:"pre"
Splitter:
sizable_from:"left"
Button:
text: "pre"
on_press: root.manager.current="abc1"
Your kv file is being loaded twice. Once by you Builder.load_file() and once by the App (see documentation). Just remove that Builder.load_file() line.
i have designed a kivy form which takes some information from the user..But none of it is getting displayed..Please tell me where I am going wrong.
My code:
import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import ScreenManager
from kivy.uix.floatlayout import FloatLayout
class Widgets(Widget):
pass
class YourDiaryApp(App):
def build(self):
return Widgets()
if __name__ == "__main__":
YourDiaryApp().run()
Kivy code:
<Widgets>:
FloatLayout:
Label:
text:'What do you want me to call you'
TextInput:
id:username
Label:
text:'What do you wanna call me'
TextInput:
id:diaryname
Label:
text:'What will be your password'
TextInput:
id:password
Button:
text:'Complete Setup'
Is your KV file named "yourDiaryApp.kv"? From the docs:
There are two ways to load Kv code into your application:
By name convention:
Kivy looks for a Kv file with the same name as your App class in lowercase, minus “App” if it ends with ‘App’ e.g:
MyApp -> my.kv
My guess would be the KV code isn't being loaded.
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
I'm trying to make a simple application in Python 3.5 and kivy that starts with a simple screen and when you click on it, goes to another one which shows 3 lists that let you chose the data:
The Python file:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.listview import ListItemButton
from kivy.properties import ListProperty
from dataTopy import rlists
# Transition des ecrans:
class MainScreen(Screen):
pass
class AnotherScreen(Screen):
pass
class ScreenManagement(ScreenManager):
pass
presentation = Builder.load_file("ex44.kv")
#
class FirstListItemButton(ListItemButton):
pass
class SecondListItemButton(ListItemButton):
pass
class ThirdListItemButton(ListItemButton):
pass
class Ex44(BoxLayout):
d1 = ListProperty([str(i) for i in range(1990,2014)] )
d2 = ListProperty(['']*100)
d3 = ListProperty(['']*100)
def change(self,c):
try: self.d2,self.d3 = rlists(int(c.text))
except:
import os
CurDir = os.getcwd()
print('Can not find data in ' + CurDir)
def change1(self,c):
print('M => '+c.text)
def change2(self,c):
print('F => '+c.text)
class Ex44App(App):
def build(self):
return presentation
if __name__ == '__main__':
Ex44App().run()
The kivy file:
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
#: import ListAdapter kivy.adapters.listadapter.ListAdapter
#: import ex44 ex44
ScreenManagement:
transition: FadeTransition()
MainScreen:
AnotherScreen:
<MainScreen>:
name: "main"
Button:
on_release: app.root.current = "other"
text: "Next Screen"
font_size: 50
<AnotherScreen>:
name: "other"
BoxLayout:
Ex44
Button:
color: 0,1,0,1
font_size: 25
size_hint: 0.3,0.2
text: "Back Home"
on_release: app.root.current = "main"
pos_hint: {"right":1, "top":1}
<FirstListItemButton>:
on_press: app.root.change(*args)
<SecondListItemButton>:
on_press: app.root.change1(*args)
<ThirdListItemButton>:
on_press: app.root.change2(*args)
<Ex44>:
ListView:
adapter:
ListAdapter(data=root.d1,
selection_mode='single',
cls=ex44.FirstListItemButton)
ListView:
adapter:
ListAdapter(data=root.d2,
selection_mode='single',
cls=ex44.SecondListItemButton)
ListView:
adapter:
ListAdapter(data=root.d3,
selection_mode='single',
cls=ex44.ThirdListItemButton)
When I try to run the app, it tells me: "Unknown class "
It is weird because the class Ex44 works alone but not when I'm trying to add it into the main application logic.
I've tried to return a widget instead of a BoxLayout for the class, to return Ex44 alone in the kivy file, etc. but I always get the same error in return.
Is it possible to return a BoxLayout inside another one in Kivy?
You are building the kv file too soon (before the class is defined). move the Builder.from_file call to the build method
...
def build(self):
return Builder.load_file("ex44.kv")
I've been searching around for answers on stackoverflow for a couple of days now to be honest but I could not find the thing for me, lets say I have a text file named bind.txt with a couple lines of text, how can i load that text file to a kivy label? be it directly or indirectly. I've been trying to teach myself python and this is kind of in the way of me building my first app. Thank you in advance, and heres the code.
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
class MainScreen(Screen):
pass
class MainLabel(ScrollView):
text = StringProperty("")
class ScreenManagement(ScreenManager):
pass
presentation = Builder.load_file("bind.kv")
class MainApp(App):
def build(self):
return presentation
if __name__ == "__main__":
MainApp().run()
And the kv file.
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManagement:
transition: FadeTransition()
MainScreen:
<MainLabel>:
text: #bind.txt here, somehow..
Label:
text: root.text
font_size: 15
text_size: self.width, None
size_hint_y: None
height: self.texture_size[1]
<MainScreen>:
name: "main"
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: 'img/Fundal.png'
MainLabel
You can open file in reading mode, store contents in a variable and assign it to the text property.
For an example
with open("bind.txt") as f:
contents = f.read()
main_label.text = contents # main_label is an instance of kivy's Label class.