so i'm trying to learn simple Kivy GUI to send SMS with a class i already made earlier, it works except the window freezes for a couple seconds, any tips on how to make the button press smooth and not freeze at all? Keep in mind the code below is just the beginning of the application, just to make sure it actually works. Thanks in advance.
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.uix.progressbar import ProgressBar
from kivy.config import Config
from sms_funksjon import SendSMS
Config.set('graphics', 'resizable', '0') # 0 being off 1 being on as in true/false
Config.set('graphics', 'width', '500')
Config.set('graphics', 'height', '200')
class MyGrid(GridLayout):
def __init__(self, **kwargs):
super(MyGrid, self).__init__(**kwargs)
self.cols = 1
self.inside = GridLayout()
self.inside.cols = 2
self.inside.add_widget(Label(text="Mobil nummer: "), pow(50, 50))
self.mobile_number = TextInput(multiline=False)
self.inside.add_widget(self.mobile_number)
self.inside.add_widget(Label(text="Tekst: "))
self.tekst = TextInput(multiline=False)
self.inside.add_widget(self.tekst)
self.add_widget(self.inside)
self.pb = ProgressBar(max=100)
self.pb.value = 0
self.add_widget(self.pb)
self.submit = Button(text="Send", font_size=40)
self.submit.bind(on_press=self.pressed)
self.add_widget(self.submit)
def pressed(self, instance):
nummer = str(self.mobile_number.text)
tekst = str(self.tekst.text)
SendSMS(nummer, tekst)
self.clear_everything()
def clear_everything(self):
self.mobile_number.text = ""
self.tekst.text = ""
class Main(App):
def build(self):
return MyGrid()
Main().run()
Related
There is no such error shown in the Python output shell.
What I want is that , the first page should be the Login page or as here, the "ConnectingPage" then Welcome Should Be shown, and then at last a Set of buttons named from 0 to 99 be shown.
Here is the code :
import kivy
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen, FallOutTransition
from kivy.uix.scrollview import ScrollView
from kivy.properties import StringProperty
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.core.window import Window
class ConnectingPage(GridLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.cols = 2
self.add_widget(Label(text = "Usename:"))
self.username = TextInput(multiline=False)
self.add_widget(self.username)
self.add_widget(Label(text = "Password:"))
self.password = TextInput(multiline=False,password = True)
self.add_widget(self.password)
self.joinbutton = Button(text="Join")
self.joinbutton.bind(on_release = self.click_join_button)
self.add_widget(Label())
self.add_widget(self.joinbutton)
def click_join_button(self, instance):
username = self.username.text
password = self.password.text
#if username == "venu gopal" and password == "venjar":
MyApp.screen_manager.current = "Info"
MyApp.screen_manager.current = "Chat"
class InfoPage(GridLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.cols = 1
self.message = Label(text = "welcome",halign="center", valign="middle", font_size=30)
self.add_widget(self.message)
class SomeApp(GridLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
layout = GridLayout(cols=1, spacing=10, size_hint_y=None)
# Make sure the height is such that there is something to scroll.
layout.bind(minimum_height=layout.setter('height'))
for i in range(100):
btn = Button(text=str(i), size_hint_y=None, height=40)
layout.add_widget(btn)
root = ScrollView(size_hint=(1, None), size=(Window.width, Window.height))
root.add_widget(layout)
if __name__ == "__main__":
runTouchApp(root)
class MyApp(App):
screen_manager = ScreenManager(transition=FallOutTransition(duration=2)) # this make screen_manager a class vaiable
def build(self):
# self.screen_manager = ScreenManager()
self.connecting_page = ConnectingPage()
screen = Screen(name='Connect')
screen.add_widget(self.connecting_page)
self.screen_manager.add_widget(screen) # add screen to ScreenManager
# Info page
self.info_page = InfoPage()
screen = Screen(name='Info')
screen.add_widget(self.info_page)
self.screen_manager.add_widget(screen) # add screen to ScreenManager
# Chat App
self.chat_app = SomeApp()
screen = Screen(name='Chat')
screen.add_widget(self.chat_app)
self.screen_manager.add_widget(screen) # add screen to ScreenManager
# return ConnectingPage()
return self.screen_manager
if __name__ == "__main__":
MyApp().run()
The Problem is that: the set of Buttons are getting shown in the start. When the Cross is pressed to close the kivy window, then the Login page is shown and then "welcome" and then the buttons again.
I want It to show from the second Step.
What I believe is that the "line 61" in the code is making a problem. When the code is run it first shows the buttons and so on.
Please Help me find the solution for the above Problem.
Your __init__() method of the SomeApp class starts an App running with the lines:
if __name__ == "__main__":
runTouchApp(root)
and that runTouchApp() does not return until that App completes. So when you run your code, it stops a the line:
self.chat_app = SomeApp()
To fix that, just replace:
if __name__ == "__main__":
runTouchApp(root)
with:
self.add_widget(root)
I am using kivy as my GUI tools for my python program.
when I want to create a table,in which there is a column containing dropdown list to make select value easier.
However, I can not make it works correctly.
The following is my code.
import kivy
kivy.require('1.10.0')
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.dropdown import DropDown
from kivy.uix.gridlayout import GridLayout
sel =["A","B","C"]
class MyGrid(GridLayout):
def __init__(self, **kwargs):
super(MyGrid, self).__init__(**kwargs)
self.redraw()
def redraw(self):
self.clear_widgets()
self.rows = 5
self.cols =2
for i in range(5):
label = Label(text="cell"+str(i+1))
self.add_widget(label)
drpName = DropDown()
btnName = Button(text="B",size_hint=(None, None))
for e in sel:
btn=Button(text=e, size_hint_y=None, height=btnName.height)
btn.bind(on_release=lambda btn:drpName.select(btn.text))
drpName.add_widget(btn)
btnName.bind(on_release=drpName.open)
drpName.bind(on_select=lambda instance, x: setattr(btnName, 'text', x))
self.add_widget(btnName)
class testApp(App):
def build(self):
return MyGrid()
if __name__=="__main__":
testApp().run()
Only a part of button open the pull down list and all of the selected value will replace the text of last button.
Could you help me out. Thanks in advance.
After reading post Python Lambda in a loop[Building Dropdowns Dynamically in Kivy, I can make my program works.
Thanks for valuable post.
import kivy
kivy.require('1.10.0')
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.dropdown import DropDown
from kivy.uix.gridlayout import GridLayout
sel =["A","B","C"]
class MyGrid(GridLayout):
def __init__(self, **kwargs):
super(MyGrid, self).__init__(**kwargs)
self.redraw()
def redraw(self):
self.clear_widgets()
self.rows = 5
self.cols =2
drpName = []
for i in range(5):
label = Label(text="cell"+str(i+1))
self.add_widget(label)
drpName.append(DropDown())
btnName=Button(text="B",size_hint=(None, None))
for e in sel:
btn=Button(text=e, size_hint_y=None, height=btnName.height)
btn.bind(on_release=lambda btn=btn,dropdown=drpName[i]:dropdown.select(btn.text))
drpName[i].add_widget(btn)
btnName.bind(on_release=drpName[i].open)
drpName[i].bind(on_select=lambda instance, x,btn=btnName: setattr(btn, 'text', x))
self.add_widget(btnName)
class testApp(App):
def build(self):
return MyGrid()
if __name__=="__main__":
testApp().run()
So i'm learning Kivy for a school project and i got an error when testing out Buttons. Here is my Code:
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.clock import Clock
from kivy.uix.button import Button
class übung(GridLayout):
def lol(instance):
label.disabled = False
def __init__(self):
super(übung, self).__init__
self.cols = 2
self.label = Label ("Ehrm ... lol")
label.disabled = True
self.btn1 = Button(text="Hello world 1")
self.btn1.bind(on_press=lol)
self.btn2 = Button(text="Hello world 2")
self.btn2.bind(on_press=lol)
class App(App):
def build(self):
return übung()
if __name__ == "__main__":
App().run()
The Error I'm getting is in the title(init takes 1 postitional argument but 2 were given). It is supposed to be two buttons and if you press one it say ehrm ... lol. As i said, it is just for testing purposes.
Thanks in Advance,
me
You have several errors. The error you display is because you have to pass the argument (text) to the Label constructor by name:
self.label = Label (text="Ehrm ... lol")
Your code should look something like this:
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
class übung(GridLayout):
def __init__(self, **kwargs):
super(übung, self).__init__(**kwargs)
self.cols = 2
self.label = Label(text = "Ehrm ... lol")
self.label.disabled = True
self.btn1 = Button(text="Hello world 1")
self.btn1.bind(on_press=self.lol)
self.btn2 = Button(text="Hello world 2")
self.btn2.bind(on_press=self.lol)
self.add_widget(self.label)
self.add_widget(self.btn1)
self.add_widget(self.btn2)
def lol(self, event):
self.label.disabled = False
class App(App):
def build(self):
return übung()
if __name__ == "__main__":
App().run()
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.stacklayout import StackLayout
from kivy.uix.modalview import ModalView
from kivy.config import Config
from ai import Ai
from random import randint
class TicTacToe(StackLayout): #StackLayout explanation: https://kivy.org/docs/api-kivy.uix.stacklayout.html
states = [1, 2, 3, 4, 5, 6, 7, 8, 9]
choices = ["X","O"]
def __init__(self, **kwargs):
super(TicTacToe, self).__init__(**kwargs)
self.init_players();
for x in range(9): # range() explanation: http://pythoncentral.io/pythons-range-function-explained/
bt = Button(text=' ', font_size=200, width=200, height=200, size_hint=(None, None), id=str(x+1))
bt.bind(on_release=self.btn_pressed)
self.add_widget(bt)
view = ModalView(auto_dismiss=False)
view.add_widget(Label(text='Hello world'))
view.open()
def init_players(self):
rand_choice = randint(0,1);
self.bot = Ai(self.choices[rand_choice]);
self.player = self.choices[0] if rand_choice == 1 else self.choices[1]
def btn_pressed(self, button):
button.text="X"
self.states[int(button.id)-1] = "X"
print(self.states)
class MyApp(App):
title = 'Tic Tac Toe'
def build(self):
Config.set('graphics', 'width', '600')
Config.set('graphics', 'height', '600')
return TicTacToe()
if __name__ == '__main__':
MyApp().run()
This is my simple code, what I am trying to do is, instead of ModalView to have a Popup saying "Hello, you started this game with X". The problem is that when I use Popup (with ModalView too), the window is being displayed behind every button. When I call the Popup after a click though, the Popup is shown properly, but I want to do this on Window initialization.
I suggest you simply open the pop-up after opening your view like
view = ModalView(auto_dismiss=False)
view.add_widget(Label(text='Hello world'))
view.open()
popup = Popup(
title=title,
content=content,
auto_dismiss=True)
popup.open()
I am new to kivy.In the below code, Once the login successfull, I am unable to move from login screen to MenuScreen (self.parent.current = MenuScreen()).
How to fix this?.
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.popup import Popup
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class LoginScreen(GridLayout):
def __init__(self, **kwargs):
super(LoginScreen, self).__init__(**kwargs)
self.labelUsername = Label(text='User Name : ',pos = (200,300))
self.add_widget(self.labelUsername)
self.inputUser = TextInput(pos = (300,340), multiline=False,height=40,width = 150)
self.add_widget(self.inputUser)
self.labelPassword = Label(text='Password :',pos = (200,250))
self.add_widget(self.labelPassword)
self.inputPasswd = TextInput(pos = (300,280), height=40,width = 150,password=True, multiline=False)
self.add_widget(self.inputPasswd)
self.btnLogin = Button(text = 'Login',pos = (320,230),width = 100,height = 30 )
self.add_widget(self.btnLogin)
self.btnLogin.bind(on_press=self.get_user_passwd)
def get_user_passwd(self,*args):
username, passwd = self.inputUser.text,self.inputPasswd.text
if username == passwd:
self.parent.current = MenuScreen()
else:
pop_up = Popup(title="username and password should be the same.", size_hint=(.3, .3))
pop_up.open()
class MenuScreen(BoxLayout):
def __init__(self,*args):
l = BoxLayout(cols="2")
btn = Button(text="ad")
l.add_widget(btn)
print "flag"
class MyApp(App):
def build(self):
screen_manager = ScreenManager()
#screen_manager.add_widget(LoginScreen(name='LoginScreen'))
#screen_manager.add_widget(MenuScreen(name='menu'))
return LoginScreen()
if __name__ == '__main__':
MyApp().run()