Kivy Popup Shows Same Buttons as Main Screen - python

I'm very new to Kivy (been using for about four hours...) and I've hit a wall with popups.
I have a main screen which has four buttons in a float layout. On press down I want the 'MOVE' button to open a popup. Now I've got this working but the popup contains the same four buttons as my mainscreen.
This is my Python code:
def show_movepop():
show = MovePop()
movepopWindow = Popup(title="Move", content=show, size_hint=(None, None),size=(400,400))
movepopWindow.open()
class MovePop(FloatLayout):
pass
class MainWindow(Screen):
def movebtn(self):
show_movepop()
class StatsWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
kv = Builder.load_file("gamegui.kv")
class MainFloatApp(App):
def build(self):
return kv
if __name__ == "__main__":
MainFloatApp().run()
and this is my .kv file:
WindowManager:
MainWindow:
StatsWindow:
<Button>
font_size:40
color:0.3,0.6,0.7,1
size_hint: 0.5, 0.1
<MainWindow>:
name: "mainscreen"
FloatLayout
Button:
text: "MOVE"
id: move
pos_hint: {"x":0, "y":0.1}
on_release: root.movebtn()
Button:
text: "ACTION"
id: action
pos_hint: {"x":0.5, "y":0.1}
Button:
text: "EXAMINE"
id: examine
pos_hint: {"x":0, "y":0}
Button:
text: "STATS"
id: stats
pos_hint: {"x":0.5, "y":0}
on_release:
app.root.current = "statsscreen"
root.manager.transition.direction = "left"
<StatsWindow>:
name: "statsscreen"
Button:
text: "Back"
on_release:
app.root.current = "mainscreen"
root.manager.transition.direction = "right"
<MovePop>:
Button:
text: "!"
pos_hint: {"x":0.1, "y":0.5}
on_release:
Apologies in advance if the above is super dirty, I'm not very efficient :')
All suggestions appreciated!

Okay so I don't know why but it was the FloatLayout that was causing the problem.
Changed
class MovePop(FloatLayout):
pass
to:
class MovePop(AnchorLayout):
pass
BoxLayout also got rid of the duplicate buttons but I couldn't arrange the content on the popup the way I wanted in that layout.

Related

Windwomanager with TappedPanels

main.py:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.tabbedpanel import TabbedPanel
class Test(TabbedPanel):
pass
class MainWindow(Screen):
pass
class SecondWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
kv = Builder.load_file("settings.kv")
class MyMainApp(App):
def build(self):
return kv
if __name__ == "__main__":
MyMainApp().run()
settings.kv:
WindowManager:
MainWindow:
SecondWindow:
Test:
<MainWindow>:
name: "main"
GridLayout:
cols:2
Button:
text: "New Order"
on_release:
app.root.current = "tabs"
root.manager.transition.direction = "left"
Button:
text: "Basket"
on_release:
app.root.current = "second"
root.manager.transition.direction = "left"
<SecondWindow>:
name: "second"
Button:
text: "Go Back"
on_release:
app.root.current = "main"
root.manager.transition.direction = "right"
<Test>:
name: "test"
size_hint: .5, .5
pos_hint: {'center_x': .5, 'center_y': .5}
do_default_tab: False
TabbedPanelItem:
text: 'first tab'
Label:
text: 'First tab content area'
TabbedPanelItem:
text: 'tab2'
BoxLayout:
Label:
text: 'Second tab content area'
Button:
text: 'Button that does nothing'
TabbedPanelItem:
text: 'tab3'
RstDocument:
text:
'\\n'.join(("Hello world", "-----------",
"You are in the third tab."))
I want a transition that starts with a button press that leads to a tabbed overlay, However the screenmanager doesnt accept the use of tabbedpanel
kivy.uix.screenmanager.ScreenManagerException: ScreenManager accepts only Screen widget.
How can I achieve this effect, I am new to kivy. I want one button press to go to a another normal screen while the other one to open a tabbed screen,
Just add a TabblePanel to one of your Screens.
<BlaBlaScreen>:
name: 'BlaBla'
TabbedPanel:
do_default_tab: False
TabbedPanelItem:
text: "something"
Just put your TabbedPanel in a Screen.
Good day. Use your screen manager to switch between each screen. The screen then contains your unique layout of whatever you placed in it. That could include a screen with tabs.

KivyMD MDNavigationRail, press icons

I was trying to use a new feature in KivyMD, the MDNavigationRail and wanted to give the icons in it a function. The goal is that the user could change to the desired screen by pressing the icon that represents it. I gave the icon an on_press. But something goes wrong, I get an error; ValueError: MDNavigationRail.state is set to an invalid option 'down'. Must be one of: ['close', 'open']. The rail should be open or closed I guess, isn't it possible to give it a function? Furthermore, I would want to know if it is possible to not break the text. If anyone could help me out, it would be very nice!
My .py file
from kivy.uix.screenmanager import ScreenManager
from kivymd.app import MDApp
from kivymd.uix.screen import MDScreen
class Screen1(MDScreen):
def screen2(self):
self.manager.current = 'screen2'
class Screen2(MDScreen):
def screen1(self):
self.manager.current = 'screen1'
def rail_open(self):
if self.ids.rail.state == "open":
self.ids.rail.state = "close"
else:
self.ids.rail.state = "open"
class MyScreenManager(ScreenManager):
def __init__(self, **kwargs):
super(MyScreenManager, self).__init__(**kwargs)
class Test(MDApp):
def build(self):
return MyScreenManager()
Test().run()
My kv file
<MyScreenManager>:
Screen1:
id: screen1
name: 'screen1'
Screen2:
id: screen2
name: 'screen2'
<Screen1>:
id: screen1
MDFloatLayout:
MDRectangleFlatButton:
text: "Change to screen 2"
on_press: root.screen2()
pos_hint: {'center_x':0.5, 'center_y':0.5}
<Screen2>:
id: screen2
MDBoxLayout:
orientation: "vertical"
MDToolbar:
left_action_items: [["menu", lambda x: root.rail_open()]]
MDBoxLayout:
MDNavigationRail:
id: rail
elevation: 1
use_resizeable: True
MDNavigationRailItem:
icon: "home"
text: "homepage"
on_press: root.screen1()
MDNavigationRailItem:
icon: ""
text: ""
MDFloatLayout:
MDTextField:
id: field1
hint_text: "Enter something:"
size_hint_x: 0.4
pos_hint: {'center_x':0.25,'top':0.8}
It was a bug. Already fixed - https://github.com/kivymd/KivyMD/commit/8a31b0f3ccad9c2d9ad35d80953f7396f2dc78f2

kivymd refences with ids

I'm trying to use the id function to reference text_input from MDTextField, however I can't understand how does the id function works. Does anyone know what is wrong in my code?
The first code is the Main App and the second one is where all the widgets are. I've already searched for a solution in internet, but I can't understand why my code does not work.
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen, ScreenManager
from Screen_helper import Home
class MenuScreen(Screen):
pass
class ProfileScreen(Screen):
pass
sm = ScreenManager()
sm.add_widget(MenuScreen(name='Menu'))
sm.add_widget(MenuScreen(name='Profile'))
class Mainapp(MDApp):
def build(self):
screen = Screen()
helper = Builder.load_string(Home)
screen.add_widget(helper)
key = self.root.ids.username_input
return screen
Mainapp().run()
Home = '''
ScreenManager:
MenuScreen:
ProfileScreen:
<MenuScreen>:
name: 'Menu'
MDRectangleFlatButton:
id: my_button
text: 'Profile'
pos_hint: {'center_x': 0.5, 'center_y': 0.1}
on_press: root.manager.current = 'Profile'
MDTextField:
id: username_input
input_filter: "int"
hint_text: 'CHIAVE NUMERICA'
helper_text: 'compresa tra 0 e 95'
helper_text_mode: 'on_focus'
icon_right: 'key-variant'
icon_right_color: app.theme_cls.primary_color
pos_hint: {'center_x':0.5,'center_y':0.55}
size_hint_x:None
width:230
input_filter: 'int'
<ProfileScreen>:
name: 'Profile'
MDLabel:
text: 'Welcome'
halign: 'center'
MDRectangleFlatButton:
text: 'back'
pos_hint: {'center_x': 0.5, 'center_y': 0.3}
on_press: root.manager.current = 'Menu' '''
Give an id to the MenuScreen class and then access to the widget.
Add this to the kv file.
<MenuScreen>:
id: menu
To access the widget you can now do this in python:
key = self.root.menu.ids.username_input

How to update button text after pressing button in python?

I'm trying to make an app that will eventually be able to score cards. One problem I'm trying to solve is how to update the text of a button after pressing or updating it somehow. I'm newer to python/kivy. I've found a couple similar questions, dealing with functions and such, but none dealt with .kv files and separate windows. My code is below.
main.py
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
class MainWindow(Screen):
pass
class ScoreWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
kv = Builder.load_file("mainkv.kv")
class MyMainApp(App):
def build(self):
return kv
if __name__ == "__main__":
MyMainApp().run()
mainkv.kv
WindowManager:
MainWindow:
ScoreWindow:
<MainWindow>:
name: "main"
GridLayout:
cols: 1
Button:
text: "Quick Score"
on_release:
app.root.current = "score"
root.manager.transition.direction = "left"
<ScoreWindow>:
name: "score"
GridLayout:
cols: 7
Button:
name: "three"
text: "Press to Update"
on_release:
three.text = "Updated"
Button:
text: ""
Button:
text: ""
Button:
text: ""
Button:
text: ""
Button:
text: ""
Button:
text: "Score"
on_release:
app.root.current = "main"
root.manager.transition.direction = "right"
Button:
text: ""
Button:
text: ""
Button:
text: ""
Button:
text: ""
Button:
text: ""
Button:
text: ""
Button:
text: "Go Back"
on_release:
app.root.current = "main"
root.manager.transition.direction = "right"
Your Buttons can refer to themselves in kvlang using the self keyword. So just add another line that says self.text = 'foo' in the on_release: area of your Buttons.

random screen display with kivy

i am trying to make an app, a game actually that will display random screens when the "NEXT" button is touched, so i made the screens and the labels to be displayed on each screen but i cant get it to display a random screen when the "NEXT" button is touched, it just follows a pattern, would anyone help me here? heres the code:
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
#: import Image kivy.uix.image.Image
#: import SoundLoader kivy.core.audio.SoundLoader
ScreenManagement:
transition: FadeTransition()
MainScreen:
GameScreen:
GameScreen2:
GameScreen3:
GameScreen4:
<Button>:
font_size: 12
size_hint: 0.2, 0.1
<MainScreen>:
name: "main"
FloatLayout:
Button:
text: "START GAME"
color: 1,0,1,1
pos_hint: {"x": 0, "y":0}
on_release: app.root.current = "game"
Button:
text: "QUIT"
color: 1,0,0,1
pos_hint: {"x": .8, "y": 0}
on_release: quit()
Button:
text: "SOUND"
color: 0,1,0,1
pos_hint: {"x":.2 , "y": .4}
on_press: app.play_sound1()
<GameScreen>:
name: "game"
FloatLayout:
Label:
text: "Python\nSnowden\nMr.Robot"
font_size: 40
color: 0,1,0,1
pos_hint: {"x":0, "y": 0}
Button:
text: "Home"
on_release: app.root.current = "main"
color: 1,0,0,1
pos_hint: {"right":1, "top":1}
Button:
text: "Next"
on_release: app.root.current = "game2"
color: 0,1,0,1
pos_hint: {"x":0, "y":0}
<GameScreen2>:
name: "game2"
FloatLayout:
Label:
text: "Banana\n\nOrange\n\nTea\n\nSleep"
font_size: 40
color: 0,1,0,1
pos_hint: {"x":0, "y": 0}
Button:
text: "Home"
on_release: app.root.current = "main"
color: 1,0,0,1
pos_hint: {"right":1, "top":1}
Button:
text: "Next"
on_release: app.root.current = "game3"
color: 0,1,0,1
pos_hint: {"x": 0, "y": 0}
<GameScreen3>:
name: "game3"
FloatLayout:
Label:
text: "Assembly\n\nRuby\n\nC"
font_size: 40
color: 0,1,0,1
pos_hint: {"x":0, "y":0}
Button:
text: "Home"
on_release: app.root.current = "main"
color: 1,0,0,1
pos_hint: {"right":1, "top":1}
Button:
text: "Next"
on_release: app.root.current = "game4"
color: 0,1,0,1
pos_hint: {"x": 0, "y": 0}
<GameScreen4>:
name: "game4"
FloatLayout:
Label:
text: "Prolog\n\nPygame\n\nC++"
font_size: 40
color: 0,1,0,1
pos_hint: {"x":0, "y":0}
Button:
text: "Home"
on_release: app.root.current = "main"
color: 1,0,0,1
pos_hint: {"right":1, "top":1}
the above is the kv code, sorry for the mess im an amateur when it comes to kivy
and heres the python side code:
from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.core.audio import SoundLoader
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
class MainScreen(Screen):
pass
class GameScreen(Screen):
pass
class GameScreen2(Screen):
pass
class GameScreen3(Screen):
pass
class GameScreen4(Screen):
pass
class ScreenManagement(ScreenManager):
pass
presentation = Builder.load_file("quora.kv")
class MainApp(App):
def build(self):
self.load_sounds()
return presentation
def load_sounds(self):
self.sounds = {}
for i in range(10):
fname = 'sound' + str(i+1) + '.wav'
self.sounds[i] = SoundLoader.load(fname)
def play_sound1(self):
sound = self.sounds.get(0)
if sound is not None:
sound.volume = 0.5
sound.play()
def play_sound2(self):
sound = self.sounds.get(1)
if sound is not None:
sound.volume = 0.5
sound.play()
if __name__ == "__main__":
MainApp().run()
this is the sample i made for you because the original one is way bigger than this i dont know how to use somewhat of a LOOP to generate screen so i made more that 20 screens on the original code so, and if you could help me figure out how can i STOP THE MENU SONG when i hit the "START" BUTTON, so that i can hit the "SONG" BUTTON on SCREEN1 to PLAY its SONG i would be gratefull. thanks you for the patience.
Display random screens when the "NEXT" button is touched
Use screen_names
screen_names
List of the names of all the Screen widgets added. The list is
read only.
screens_names is an AliasProperty and is read-only. It is updated
if the screen list changes or the name of a screen changes.
Snippets
#:import choice random.choice
...
Button:
text: "Next"
on_release:
root.manager.current = choice(root.manager.screen_names[1:])
color: 0,1,0,1
pos_hint: {"x":0, "y":0}
Stop / Play Music
You might want to use a ToggleButton instead of Button for playing music. Please refer to the example below.
Example
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.togglebutton import ToggleButton
from kivy.core.audio import SoundLoader
from kivy.properties import ObjectProperty
class MusicScreen(Screen):
enter = ObjectProperty(None)
text_input = ObjectProperty(None)
stop = ObjectProperty(None)
musicbutton = ToggleButton()
class ScreenManagement(ScreenManager):
pass
class MainApp(App):
sound = ObjectProperty(None, allownone=True)
def build(self):
return ScreenManagement()
def on_state(self, state, filename):
print("ONSTATE!!!")
print("\tstate=", state)
if self.sound is None:
self.sound = SoundLoader.load(filename)
# stop the sound if it's currently playing
if self.sound.status != 'stop':
self.sound.stop()
if state == "down":
self.sound.volume = .5
self.sound.play()
else: # if state == "normal":
if self.sound:
self.sound.stop()
# self.sound.unload()
self.sound = None
if __name__ == "__main__":
MainApp().run()
main.kv
#:kivy 1.11.0
<ScreenManagement>:
MusicScreen:
name: 'music'
<MusicScreen>:
text_input: text_input
id: "music"
name: "music"
BoxLayout:
size: root.size
pos: root.pos
orientation: "vertical"
FileChooserListView:
id: filechooser
rootpath: "/home/iam/Music/"
on_selection: text_input.text = self.selection and self.selection[0] or ''
TextInput:
id: text_input
size_hint_y: None
height: 50
multiline: False
ToggleButton:
size_hint: 1, .2
text: "Play/Stop By File"
on_state: app.on_state(self.state, text_input.text)
ToggleButton:
id: musicbutton
size_hint: 1, .2
text: "Play/Stop By Title"
on_state: app.on_state(self.state, text_input.text)

Categories

Resources