how to bind an exe to a gui button - kivy - python

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.

Related

kivy scroll view just shows black screen

I'm currently learning kivy and was following a tutorial, while following the tutorial I ran into a problem where uppon running my code only a black screen appears
this is what my .py file looks like:
from kivy.uix.widget import Widget
from kivy.uix.stacklayout import StackLayout
from kivy.uix.button import Button
from kivy.metrics import dp
from kivy.app import App
class stack_layout_example(StackLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
"""
# orientation without the kv
self.orientation = "lr-bt"
"""
for i in range(0, 100):
size = 100
button = Button(
text = str(i+1),
size_hint=(None, None),
# ( width , height )
size=(size, size)
)
self.add_widget(button)
class the_labApp(App):
pass
the_labApp().run()
and this is my .kv file:
scroll_view_example:
<scroll_view_example#ScrollView>:
stack_layout_example:
<stack_layout_example>:
You're missing the build method in the the_labApp app. It should be like the following code snippet:
class the_labApp(App):
def build(self):
return stack_layout_example()
Thank You very much for your answers guys... I already found the solution in the kivy discord
i just re-arrange the .kv file to
<ScrollViewExample#ScrollView>:
StackLayoutExample:
size_hint: 1, None
height: self.minimum_height
<StackLayoutExample>:
ScrollViewExample:
also the kv looks a bit different now because it now follows pep8(i think)

Kivy program: how to change focus in pycharm?

When I run a kivy program in Pycharm, the kivy window doesn't have the default kivy app title, the kivy logo in top-left nor the 3 control buttons in the top-right. It completely occupies my screen and I can't do anything anymore. I'm on Windows 10. Need help, please.
I'm using Python 3.9.0, kivy 2.0.0
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.label import Label
class MainWidget(Widget):
pass
class boxLayoutExample(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.orientation = "vertical"
item1 = Button(text='a button')
self.add_widget(item1)
item2 = Label(text='a label')
self.add_widget(item2)
item3 = Button(text='a button')
self.add_widget(item3)
class myApp(App):
pass
myApp().run()
the key is the configuration which can either be modified using the config.ini file or the config object.
on my windows system the config file is in C:\users<username>.kivy\config.ini
and you may have to change Windows Explorer to show hidden items if you want to see the .kivy directory.
inside that configuration file, the borderless property being set to 1 could cause this behavior so set this to 0. The curios thing is that by default this should have been 0 so I don't know why it would have changed.
[graphics]
borderless = 0
and you may also be interested in these properties
width = 1200
height = 600
fullscreen = 0
from kivy import Config
Config.set("graphics", "fullscreen", "0")
RTFM -> go here

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()

How to dynamically change Kivy window dimensions based on platform

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

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