My simple label in kivy is very blurry, I just wanted to know how to make it look sharper?
Here's the code:
import kivy
from kivy.app import App
from kivy.uix.label import Label
class MyApp(App):
def build(self):
return Label(text="Hello World")
if __name__ == '__main__':
MyApp().run()
Thanks.
The issue seems to be coming from you. On Windows 10, click the windows button/search, type ClearType. One of the options should say Adjust ClearType Text. Open that, and follow the wizard
Related
I am trying to learn kivy in python and I have built a small program that should display hello world on the screen but when I run it I get:
cannot import name 'app' from 'kivy.app'
code:
from kivy.app import app
from kivy.uix.label import label
class MyApp(App):
def build(self):
return label(text="hello world")
if __name__ == "__main__":
MyApp.run()
and I am running this in a virtual enviroment
Good day. As mentioned by buran in the comments of your question, your import code must change to:
from kivy.app import App
from kivy.uix.label import Label
Even later in your code, MyApp inherits from App.
Watch your syntax. Classes generally begin with capital letters.
I'm trying to use kivy for my project but I can't deal with it well..
I made a button, but I want that when I press him it will create another (new) button. Thanks a lot!
from kivy.app import App
from kivy.lang import builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from kivy.uix.button import Button
def createButton():
b = Button(pos=(0, 90), size_hint=(.2, .2), on_press=lambda a:ad())
return b
def ad():
"crate new button here!"
class NoobApp(App):
def build(self):
return createButton()
if __name__ == '__main__':
NoobApp().run()
In your ad() method add a line to create a Button and add it to the root of the app:
def ad():
print("crate new button here!")
App.get_running_app().root.add_widget(Button(text='hi'))
Note that this is adding a Button to a Button, (the root of the app is the original Button). Not a recommended approach. You should probably return some kind of Layout from your build() method instead.
According to the kivy document, I can remove Window's border by Window.borderless = True
(https://kivy.org/doc/stable/api-kivy.core.window.html)
However, The problem is, it still shows the border when it starts up, then it gets removed in like 0.5 second. And it seems a bit weird to me
Is it possible to remove the border at the very beginning?
The problem is caused by first reading the Config and probably configured to be border and then read your configuration, so the solution in that case is to save it in the config, so a second load of the application will no longer watch that transition.
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.config import Config
Config.read("myapp.ini")
if Config.getint('graphics', 'borderless') == 0:
Config.set('graphics', 'borderless', '1')
Config.write()
Window.borderless = True
class MyApp(App):
def build(self):
return Widget()
if __name__ == '__main__':
MyApp().run()
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.
I would like my Kivy application to be able to spawn multiple apps (i.e. new windows) on a Windows machine that can communicate with each other.
ScreenManager and Popup options will not cut it because they live in the same window..I need to be able to drag new screens across multiple monitors and therefore need multiple windows.
Kivy docs explicitly state that "Kivy supports only one window
per application: please don't try to create more than one."
A google search produces this simple approach of simple spawning a new app from within another app, like so:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
class ChildApp(App):
def build(self):
return Label(text='Child')
class MainApp(App):
def build(self):
b = Button(text='Launch Child App')
b.bind(on_press=self.launchChild)
return b
def launchChild(self, button):
ChildApp().run()
if __name__ == '__main__':
MainApp().run()
However, when I do this, it launches the app within the same window and crashes, and my terminal spits out like crazy:
Original exception was:
Error in sys.exceptionhook:
I get the same result if instead of ChildApp().run() I do multiprocessing.Process(target=ChildApp().run()).start()
Using the subprocess library gets me closer to what I want:
# filename: test2.py
from kivy.app import App
from kivy.uix.label import Label
class ChildApp(App):
def build(self):
return Label(text='Child')
if __name__ == '__main__':
ChildApp().run()
# filename: test.py
from kivy.app import App
from kivy.uix.button import Button
import subprocess
class MainApp(App):
def build(self):
b = Button(text='Launch Child App')
b.bind(on_press=self.launchChild)
return b
def launchChild(self, button):
subprocess.call('ipython test2.py', shell=True)
if __name__ == '__main__':
MainApp().run()
This spawns the child window without error, however now the main window is locked (white canvas) and if I close the child window, it just gets reopened.
They need to be able pass data between one another. Any ideas on how to do this correctly in Windows? This post seems to suggest that this is possible but I'm not sure where to start.
I tried baconwichsand's code and can confirm with Python 3.6 and Windows 10 it does not work. Apparently only top level object classes can be pickled, and since both apps inherit from the App class python throws an error. However a top level definition that simply executes the ChildApp().run() command can be pickled and works. Here is my working code.
import multiprocessing
from kivy.app import App
from kivy.uix.label import Label
class MainApp(App):
def build(self):
return Label(text='Main App Window')
class OtherApp(App):
def build(self):
return Label(text='Other App Window')
def open_parent():
MainApp().run()
def open_child():
OtherApp().run()
if __name__ == '__main__':
a = multiprocessing.Process(target=open_parent)
b = multiprocessing.Process(target=open_child)
a.start()
b.start()
And here is the code I am using, including the Builder to use a shared .kv file for both windows.
import multiprocessing
from kivy.lang import Builder
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.widget import Widget
class MainRoot(Widget):
pass
class OtherRoot(Widget):
pass
class MainApp(App):
def build(self):
Builder.load_file('B:\Python_Codes\Testing Grounds\shared.kv')
main = MainRoot()
return main
class OtherApp(App):
def build(self):
Builder.load_file('B:\Python_Codes\Testing Grounds\shared.kv')
other = OtherRoot()
return other
def open_parent():
MainApp().run()
def open_child():
OtherApp().run()
if __name__ == '__main__':
a = multiprocessing.Process(target=open_parent)
b = multiprocessing.Process(target=open_child)
a.start()
b.start()
I'm not sure why it doesn't work with multiprocessing (I've never tried it), but it should at least work with subprocess. The reason your main window is locked is because subprocess.call blocks the thread that calls it while it waits for the subprocess to finish and return a result.
You want to use subprocess.Popen instead, which does not block.
bj0's answer regarding subprocess was correct.
Even better, I figured out how to do this via multiprocessing, which allows better communication and passing of information between apps. It wasn't working before because I did multiprocessing.Process(target=ChildApp().run()).start() when it should be multiprocessing.Process(target=ChildApp().run).start(). The following works
# filename: test.py
from kivy.app import App
from kivy.uix.button import Button
from test2 import ChildApp
import multiprocessing
class MainApp(App):
def build(self):
b = Button(text='Launch Child App')
b.bind(on_press=self.launchChild)
return b
def launchChild(self, button):
app = ChildApp()
p = multiprocessing.Process(target=app.run)
p.start()
if __name__ == '__main__':
MainApp().run()
# filename: test2.py
from kivy.app import App
from kivy.uix.label import Label
class ChildApp(App):
def build(self):
return Label(text='Child')
if __name__ == '__main__':
ChildApp().run()
Working on Ubuntu Python 3.10
Calling a subprocess in a multiprocess.
main.py
import multiprocessing
import subprocess
import shlex
#my kivy code..
def sub_window():
subprocess.call(shlex.split('python3 test.py'))
if __name__ == '__main__':
b = multiprocessing.Process(target=sub_window)
b.start()
MyApp().run()
test.py
#another kivy app script..
OtherApp().run()