Popup in Kivy Python - show in front - python

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

Related

The Screen Manager is not returning Desired Output in Kivy Python

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)

How to access the custom class in python?

I am newbies to programming, just picking up python. I have a very simple program which is showing a popup message, and it seems OK:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.popup import Popup
class t6App(App):
def build(self):
lb = Label(text="Welcome Here",size_hint=(1,0.8),pos_hint={'left':1,'top':1})
bt = Button(text="Press Me", size_hint=(1,0.1),pos_hint={'left':1,'x':0})
fl = FloatLayout()
fl.add_widget(lb)
fl.add_widget(bt)
cp = Popup(title='Guys', content=fl)
return cp
t6App().run()
Then I wanted to modify it, and add more things to it, the popup didn't show then: (2 programs always the same)
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.popup import Popup
class CustPop(Popup):
def build(self):
lb = Label(text="Welcome Here",size_hint=(1,0.8),pos_hint={'left':1,'top':1})
bt = Button(text="Press Me", size_hint=(1,0.1),pos_hint={'left':1,'x':0})
fl = FloatLayout()
fl.add_widget(lb)
fl.add_widget(bt)
cp = Popup(title='Guys', content=fl)
return cp
class t6App(App):
def build(self):
cp1 = CustPop()
return cp1
t6App().run()
The codes do run, but it seems like the layout /button /label in CustPop() can't be accessed (became a blank popup). My question is why and how to make it work?
Thx guys. I am appreciated.
You have defined a build() method in your CustPop, but unlike the App class, that build() method is not automatically called. You should put that code (with a couple minor changes) in the __init__() method.
class CustPop(Popup):
def __init__(self, **kwargs):
super(CustPop, self).__init__(**kwargs)
lb = Label(text="Welcome Here", size_hint=(1, 0.8), pos_hint={'left': 1, 'top': 1})
bt = Button(text="Press Me", size_hint=(1, 0.1), pos_hint={'left': 1, 'x': 0})
fl = FloatLayout()
fl.add_widget(lb)
fl.add_widget(bt)
# cp = Popup(title='Guys', content=fl)
self.title = 'Guys'
self.content = fl

how to Change colour of a particular button in gridLayout in kivy

I am trying to make tambola coin picker with Python and Kivy and I am new to kivy.
Here, I created gridlayout buttons from 1 to 90. I want to change the color of particular button in gridlayout when its number is picked. I am facing issues to update gridlayout with new colored button. Here I am attaching my code. screenshot
#!/usr/bin/python
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.graphics import Color
import random
coins = random.sample(range(1,91), 90)
#print(coins)
picked_coins=[]
current_coin=0
#print(picked_coins)
class Housie(FloatLayout):
def __init__(self,**kwargs):
super(Housie,self).__init__(**kwargs)
self.title = Label(text="Housie Coin Picker",font_size = 50,size_hint=(1, .55),pos_hint={'x':0, 'y':.45})
self.main_label = Label(text = "Click PICK NUMBER", size_hint=(1, .60),pos_hint={'x':0, 'y':.35})
self.picked_ones = Label(text = "picked_coins", size_hint=(1, .40),pos_hint={'x':0, 'y':.40})
self.help_button = Button(text = "PICK NUMBER", size_hint=(.3, .1),pos_hint={'x':.65, 'y':.1},on_press = self.update)
self.add_widget(self.title)
self.add_widget(self.main_label)
self.add_widget(self.picked_ones)
self.add_widget(self.help_button)
self.add_widget(self.userinterface())
def userinterface(self):
self.layout = GridLayout(cols = 10,size_hint=(.50, .50))
for i in range(1,91):
self.layout.add_widget(Button(background_color=(1,0,0,1),text =str(i)))
return self.layout
def update(self,event):
for coin in coins:
if coin not in picked_coins:
current_coin=coin
picked_coins.append(coin)
self.main_label.text = str(coin)
for i in self.layout.children:
if i.text == str(coin):
#What to do Here?
break
self.picked_ones.text = "Picked coins = {}".format(" ".join(str(sorted(picked_coins))))
class app1(App):
def build(self):
return Housie()
if __name__=="__main__":
app1().run()
You can bind a method to each Button like this:
def userinterface(self):
self.layout = GridLayout(cols = 10,size_hint=(.50, .50))
for i in range(1,91):
self.layout.add_widget(Button(background_color=(1,0,0,1),text=str(i), on_release=self.butt_pressed))
return self.layout
def butt_pressed(self, button):
button.background_normal = ''
button.background_color = (1,0,0,1)
Th butt_pressed() method changes the background color of the pessed Button.

Kivy [Freezes on button press]

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

Create kivy dropdown list but only a part of them can be opened

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

Categories