Created
December 23, 2019 09:21
-
-
Save d1y/ac2b534c105b6fcd848e0e7ed0471da0 to your computer and use it in GitHub Desktop.
kivy demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import kivy | |
| from kivy.app import App | |
| from kivy.uix.label import Label | |
| from kivy.uix.gridlayout import GridLayout | |
| from kivy.uix.textinput import TextInput | |
| # to use buttons: | |
| from kivy.uix.button import Button | |
| kivy.require("1.10.1") | |
| class ConnectPage(GridLayout): | |
| # runs on initialization | |
| def __init__(self, **kwargs): | |
| super().__init__(**kwargs) | |
| self.cols = 2 # used for our grid | |
| # Read settings from text file, or use empty strings | |
| if os.path.isfile("prev_details.txt"): | |
| with open("prev_details.txt","r") as f: | |
| d = f.read().split(",") | |
| prev_ip = d[0] | |
| prev_port = d[1] | |
| prev_username = d[2] | |
| else: | |
| prev_ip = '' | |
| prev_port = '' | |
| prev_username = '' | |
| self.add_widget(Label(text='IP:')) # widget #1, top left | |
| self.ip = TextInput(text=prev_ip, multiline=False) # defining self.ip... | |
| self.add_widget(self.ip) # widget #2, top right | |
| self.add_widget(Label(text='Port:')) | |
| self.port = TextInput(text=prev_port, multiline=False) | |
| self.add_widget(self.port) | |
| self.add_widget(Label(text='Username:')) | |
| self.username = TextInput(text=prev_username, multiline=False) | |
| self.add_widget(self.username) | |
| # add our button. | |
| self.join = Button(text="Join") | |
| self.join.bind(on_press=self.join_button) | |
| self.add_widget(Label()) # just take up the spot. | |
| self.add_widget(self.join) | |
| def join_button(self, instance): | |
| port = self.port.text | |
| ip = self.ip.text | |
| username = self.username.text | |
| with open("prev_details.txt","w") as f: | |
| f.write(f"{ip},{port},{username}") | |
| print(f"Joining {ip}:{port} as {username}") | |
| class EpicApp(App): | |
| def build(self): | |
| return ConnectPage() | |
| if __name__ == "__main__": | |
| EpicApp().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment