Has anyone else found that the minimum_height value is sometimes ignored. Here is some sample code. At times it functions just as expected at other times it does not. I can find no pattern to this behavior. To recreate the issue please run the code, close it and run again. It usually takes me no more than 3 tries to get the issue to appear. What you should see is sometimes the buttons at the bottom of the screen take on the minimum height that matches the font size and at other times the buttons take on the default height of 100.
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.app import runTouchApp
main_layout = BoxLayout(orientation="vertical")
filler_label = Label(text='Space Filler Label',halign='center',valign='middle',size_hint=(1,None))
filler_layout = GridLayout(cols=1,size_hint=(1,1))
filler_layout.bind(minimum_height=filler_layout.setter('height'))
filler_layout.add_widget(filler_label)
button_layout = GridLayout(cols=3,size_hint_y=None)
button_layout.bind(minimum_height=button_layout.setter('height'))
button1 = Button(text='Button1',size_hint_y=None)
button1.bind(texture_size=button1.setter('size'))
button2 = Button(text='Button2',size_hint_y=None)
button2.bind(texture_size=button2.setter('size'))
button3 = Button(text='Button3',size_hint_y=None)
button3.bind(texture_size=button3.setter('size'))
button_layout.add_widget(button1)
button_layout.add_widget(button2)
button_layout.add_widget(button3)
main_layout.add_widget(filler_layout)
main_layout.add_widget(button_layout)
runTouchApp(main_layout)
Related
I have tried to create a layout with multiple rows, where you can count up and down. The count is displayed in the label next to the buttons.
It is a part of a larger layout and application.
I get the layout that I want, but nothing happens when I press the buttons.
I have watched a lot of videos and searched the internet and encountered the .bind but I still haven't fixed it. I have also tried to make changes in the 'def add' and 'def sub'. I am not sure the *args should be there, but if i remove it I get a Traceback: TypeError: Countdown.add() takes 1 positional argument but 2 were given
I hope you can help on my way :)
The python file:
from kivy.app import App
from kivy.core.window import Window
from kivy.graphics.context_instructions import Color
from kivy.graphics.vertex_instructions import Ellipse, Line, Rectangle
from kivy.metrics import dp
from kivy.uix.scrollview import ScrollView
from kivy.properties import (BooleanProperty, NumericProperty, ObjectProperty,
StringProperty)
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.stacklayout import StackLayout
from kivy.uix.textinput import TextInput
from kivy.uix.widget import Widget
Window.clearcolor =(1,1,1,1)
class Countdown(GridLayout):
my_text = StringProperty("0")
count = 0
def __init__(self, **kwargs):
super().__init__(**kwargs)
for i in range(0,10):
self.cols = 3
self.btn1=Button(text="+")
self.btn1.bind(on_press=self.add)
self.add_widget(self.btn1)
self.btn2=Button(text="-")
self.btn2.bind(on_press=self.sub)
self.add_widget(self.btn2)
self.lb=Label(text=self.my_text, color=[0,0,0,1])
self.add_widget(self.lb)
def add(self,*args):
self.count +=1
self.my_text = str(self.count)
def sub(self, *args):
self.count -=1
self.my_text = str(self.count)`
Allright, I just wrote a programme specific for my data analysis purposes and convert it to exe and when i double click on it, it works fine now except showing the console and all the other things it is doing. Instead double clicking on the exe file, i developped a very simple kivy gui which is as follows;
from cProfile import label
from email.mime import image
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
class Data_Col(App):
def build(self):
self.window = GridLayout()
self.window.cols = 1
self.window.size_hint = (0.5, 0.5)
self.window.pos_hint = {"center_x": 0.5, "center_y" : 0.5}
# add widgets to window
# image widget
self.window.add_widget(Image(source="logo.png"))
# label widget
self.greeting = Label(text="Bonjour!!!", font_size = 18, bold = True)
self.window.add_widget(self.greeting)
# button widget
self.button = Button(text="Run Data-Col", size_hint = (0.3, 0.3), bold = True, background_color = '0175E8', background_normal = "")
self.window.add_widget(self.button)
return self.window
if __name__ == "__main__":
Data_Col().run()
what i want is when the user clicks on the button, it runs the exe file which is in a different folder in my pc. So i need to give a path too to the button to go and tricks the execution of exe file. But don't know how to do it, if anyone knows i would appreciate it. thanks
I don't know much about Kivy, but I can help you run an .EXE file.
This is the code I wrote:
import os
def run_exe(file): # This is the function to run
os.system("start "+file)
run_exe("path/to/file.exe") # Replace "path/to/file.exe" with the path to your file (E.G. "C:/Users/Hipposgrumm/Documents/helloworld.exe").
Hopefully, that works. I'm more of a TKinter user so tell me if this isn't usable.
I'm trying to create a user-interface for a personal assistant.
I want the user to input a text and when he presses enter,i want to do something(' say print a text') and also automatically clear the input field.
This is my code:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
class TetraApp(App):
def build(self):
Window.size=(875,600)
Window.clearcolor = (0, 1, 1, 1)
b = BoxLayout(orientation ='vertical')
self.t = TextInput(hint_text='Say Something...', size_hint=(1,0.1), multiline=False)
#the multiline disables on enter. i want it to do a process on enter.
b.add_widget(self.t)
# code here to go to enterClicked() when enter is pressed and to clear input field
Window.borderless=True
return b
def enterClicked(self):
if 'hello' in self.t.text:
print("hello user")
if __name__=='__main__':
app=TetraApp()
app.run()
I couldnt find any tutorials for this.
You can try to bind an action to your TextInput like this:
self.t = TextInput(hint_text='Say Something...', size_hint=(1,0.1),multiline=False)
self.t.bind(on_text_validate=self.enterClicked)
b.add_widget(self.t)
def enterClicked(self,t):
if 'hello' in self.t.text:
print("hello user")
self.t.text=''
The on_text_validate action is triggered only in multiline=False mode when the user hits ‘enter’.
To clear the input field, try to make a method that clears the text (similar to your enterClicked) and bind this method as well to the TextInput with on_text_validate. Let me know if it worked.
I have unfortuanlly have encourtered an error in kivy and Python 3. I have not found a soultion via Google. I wanted to get text input at the very least but it does not show up. Just the text itself. Thank you for your time!
import kivy
kivy.require('1.10.1') # replace with your current kivy version !
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
class ColdKivyApp(App):
def build(self):
f = FloatLayout()
label = Label(text="Cold") #I acutally orginally called it Zone unitil I changed it into Cold cause it's really cold now
f.add_widget(label)
txt = TextInput(text='', focus=True, multiline=True, cursor_blink=True, background_color=(1,1,1,1))
f.add_widget(txt)
return f
if __name__ == '__main__':
ColdKivyApp().run()
It seems that there is a bug in TextInput when setting the focus in the constructor, a workaround is to set the focus an instant after the window is shown through Clock:
import kivy
kivy.require('1.10.1') # replace with your current kivy version !
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.clock import Clock
class ColdKivyApp(App):
def build(self):
f = FloatLayout()
label = Label(text="Cold")
f.add_widget(label)
txt = TextInput(multiline=True, cursor_blink=True, background_color=(1,1,1,1))
f.add_widget(txt)
Clock.schedule_once(lambda *args: setattr(txt, "focus", True))
return f
if __name__ == '__main__':
ColdKivyApp().run()
So I am just starting out with Kivy, and want to know how to
A: get the dimensions of the window (based on the platform)
The end goal is to launch the app on iOS, and there are multiple sized iPhones out there. How would I get the size of the screen of the device?
and B: somehow be able to refer to it
If I want to create a button that is centered on the screen, I need some way of getting the width_of_the_window and the length_of_the_window (these are obviously just made up variables).
Here is the code I am working on, although it is really basic so it probably won't provide much insight.
# Importing os
import os
# Importing Kivy, making sure its up to date, and importing App
import kivy
kivy.require('1.10.1')
from kivy.app import App
# Importing widgets, like buttons, drop down menus, etc
from kivy.uix.widget import Widget
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
button_one = Button(text='Test', font_size = 20, pos = (root.width-100, 100), size_hint = (.06, .06))
class calculator_app(App):
def build(self):
return button_one
calculator_object = calculator_app()
calculator_object.run()
Actually, you don't need the size of the window in order to do centering. Change your button creation line to :
button_one = Button(text='Test', font_size = 20, pos_hint={'center_x': 0.5, 'center_y': 0.5}, size_hint = (.06, .06))
Using pos_hint will position the button regardless of the window size. Have a look at the documentation for pos_hint