Reputation: 481
I have had a decent look around and haven't been able to find a solution for my problem. In my main.kv file, I have "home_screen" and "settings_screen" widget buttons that are displayed constantly (regardless of what screen the user is on).
The problem is that, when a user is not logged in and is on the "login_screen", these two buttons are there and remain active, allowing the user to simply access the "home_screen" or "settings_screen" before logging in.
What I'm trying to do is to remove these two widgets from the screen whenever the user is on the login screen. I fee like I'm on the right track but am maybe not referencing the widgets correctly. There are no errors arising, it simply doesn't do anything.
I have tried running my "if" statement and all it's necessary components in the change_screen()
function and have also tried giving it its own function (shown in my example). I have tried it with a while
statement (which seems to jam the app unless I put it in a thread). I have tried several different ways to reference the widgets and ways to write the remove_widget()
line.
I have included the most basic functioning example I could so that someone may try run it themselves. Please help.
main.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
class HomeScreen(Screen):
pass
class LoginScreen(Screen):
pass
GUI = Builder.load_file("main.kv")
class TestApp(App):
def build(self):
return GUI
def change_screen(self, screen_name):
screen_manager = self.root.ids['screen_manager']
screen_manager.current = screen_name
self.home_setting_widgets(screen_name)
def home_setting_widgets(self, screen_name):
home_button = self.root.ids["home_button"]
settings_button = self.root.ids["settings_button"]
if screen_name == "login_screen": # if i'm on the login screen
self.root.remove_widget(home_button) # remove widgets
self.root.remove_widget(settings_button)
TestApp().run()
main.kv
#:include kv/loginscreen.kv
#:include kv/homescreen.kv
GridLayout:
cols: 1
FloatLayout:
GridLayout:
rows: 1
pos_hint: {"top": 1, "left": 1}
size_hint: 1, .05
Button:
id: home_button
text: "home"
Button:
id: settings_button
text: "settings"
ScreenManager:
size_hint: 1, .95
pos_hint: {"top": .95, "left": 1}
id: screen_manager
LoginScreen:
name: "login_screen"
id: login_screen
HomeScreen:
name: "home_screen"
id: home_screen
loginscreen.kv
<LoginScreen>:
FloatLayout:
TextInput:
size_hint: .7, .08
pos_hint: {"top": .8, "center_x": .5}
TextInput:
size_hint: .7, .08
pos_hint: {"top": .7, "center_x": .5}
Button:
text: "login"
pos_hint: {"top": .2, "center_x": .5}
size_hint: .5, .08
on_release:
app.change_screen("home_screen")
homescreen.kv
<HomeScreen>:
Button:
text: "back"
pos_hint: {"top": .2, "center_x": .5}
size_hint: .5, .08
on_release:
app.change_screen("login_screen")
This is a basic layout but still reproducible. Thank you :)
Upvotes: 0
Views: 254
Reputation: 1128
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
class HomeScreen(Screen):
pass
class LoginScreen(Screen):
pass
GUI = Builder.load_file("main.kv")
class TestApp(App):
def build(self):
return GUI
def change_screen(self, screen_name):
screen_manager = self.root.ids['screen_manager']
screen_manager.current = screen_name
self.home_setting_widgets(screen_name)
def home_setting_widgets(self, screen_name):
home_button = self.root.ids["home_button"]
settings_button = self.root.ids["settings_button"]
grid_layout = self.root.children[0].children[1]
print("I remove from my widget, not from my root", grid_layout)
if screen_name == "login_screen": # if i'm on the login screen
grid_layout.remove_widget(home_button) # remove widgets
grid_layout.remove_widget(settings_button)
TestApp().run()
I agree that there should be error message/warning.
Upvotes: 1