How to dynamically change Kivy window dimensions based on platform - python

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

Related

how to bind an exe to a gui button - kivy

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.

KIVY scroll view won't show anything just a blank screen

I created a stack layout with 100 buttons in it. This alone works but when I add the stack layout under scroll view layout nothing is displayed on the screen.without using scroll view
when scroll view is used
Python code
from kivy.app import App
from kivy.metrics import dp
from kivy.uix.stacklayout import StackLayout
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
class stacklayoutex(StackLayout):
def __init__(self,**kwargs):
super().__init__(**kwargs)
for i in range(100):
b=Button(text=str(i+1),size_hint=(None,None),size=(dp(100),dp(100)))
self.add_widget(b)
class MainWidget(Widget):
pass
class TheLabApp(App):
pass
TheLabApp().run()
KV file
scrollviewex:
<scrollviewex#ScrollView>:
stacklayoutex:
size_hint: 1,None
height:4000
<stacklayoutex>:
A couple problems:
Your class names must begin with upper case. Not following this rule can cause syntax errors in your kv.
Also, indentation error in your kv, it should be:
Scrollviewex:
<Scrollviewex#ScrollView>:
Stacklayoutex:
size_hint: 1,None
height:4000
<Stacklayoutex>:

Is there a drag and drop function in KivyMD or Kivy?

im really new in Kivy and i would like to make an app where i could create a MDIconButton that is draggable, and if possible, droppable in any BoxLayout? Is that possible in KivyMD or Kivy? Also is there a Kivy function where whenever I hold down a button, it'll display some kind of small dialogue box that contains details that can be entered by the user. thanks!
Rather than use Drag-N-Drop from kivy-garden just use the DragBehavior class! It comes directly installed with Kivy and it'll save you having to install kivy-garden.
https://kivy.org/doc/stable/api-kivy.uix.behaviors.drag.html
Here is some example code of how it's used:
from kivy.uix.label import Label
from kivy.app import App
from kivy.uix.behaviors import DragBehavior
from kivy.lang import Builder
# You could also put the following in your kv file...
kv = '''
<DragLabel>:
# Define the properties for the DragLabel
drag_rectangle: self.x, self.y, self.width, self.height
drag_timeout: 10000000
drag_distance: 0
FloatLayout:
# Define the root widget
DragLabel:
size_hint: 0.25, 0.2
text: 'Drag me'
'''
class DragLabel(DragBehavior, Label):
pass
class TestApp(App):
def build(self):
return Builder.load_string(kv)
TestApp().run()

Kivy (Python) minimum_height behaves inconsistently

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)

Issues with changing color of basic Kivy app

I created a simple text-to-speech app with Kivy, using the FloatLayout option but am having trouble changing the color of the GUI without actually creating a .kv file (which I do not wish to do). The code of my app is here:
import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image
import requests
from threading import Thread
import os
class ButtonApp(App):
def talk(self):
self.info.text = self.text.text
command = "say %s" % (self.text.text)
os.system(command)
def say(self,instance):
t = Thread(target=self.talk)
t.start()
def build(self):
self.b = FloatLayout()
self.info = Label(text="Hello!", pos=(20,400) ,size_hint=(1,0.5), font_size="40sp")
self.text = TextInput(text='Hello!', pos=(20,200), size_hint=(1,0.5))
self.submit = Button(on_press=self.say,text='Submit',pos=(20,100), size_hint=(1,0.5))
self.b.add_widget(self.info)
self.b.add_widget(self.text)
self.b.add_widget(self.submit)
self.b.bind()
return self.b
if __name__ == "__main__":
ButtonApp().run()
Like I mentioned beforehand, all the suggestions I found doing prior research involved either Canvas (which I am not using), or creating a .kv file. Is there a pure python-kivy method of changing the color of a GUI?
You can do anything in pure python, though the reason you see so many kv examples is because it's easier and more concise due to being a more domain specific language, so I don't recommend avoiding it.
What kind of change do you actually want to make? For instance, you can change the background image of the Button with the background_normal or background_down properties (which take a filepath to an image), or tint its colour by setting its background_color to e.g. (1, 0, 0, 1) for red.

Categories

Resources