Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from jnius import autoclass
- # Access Android Toast class
- Context = autoclass('android.content.Context')
- Toast = autoclass('android.widget.Toast')
- String=autoclass("java.lang.String")
- Handler=autoclass("android.os.Handler")
- Looper=autoclass("android.os.Looper")
- main_handler=Handler(Looper.getMainLooper())
- def exit_app():
- root.destroy()
- def show_toast():
- # Get the text from the entry
- main_handler.post(toast_runnable)
- def toast_runnable():
- # Get the Android application context
- app_context = autoclass('org.kivy.android.PythonActivity').mActivity
- j_msg=String(entry.get())
- # Show the toast with the message
- Toast.makeText(app_context, j_msg, Toast.LENGTH_SHORT).show()
- # Create the main window
- root = tk.Tk()
- root.title("Tkinter with Toast")
- root.geometry("300x200") # Set the window size
- # Add a label
- label = tk.Label(root, text="Enter something:")
- label.pack(pady=10)
- # Add a text entry field
- entry = tk.Entry(root, width=25)
- entry.pack(pady=5)
- # Add a button to close the app
- exit_button = tk.Button(root, text="Return to Editor", command=exit_app)
- exit_button.pack(pady=10)
- # Add a button to show the toast
- toast_button = tk.Button(root, text="Show Toast", command=show_toast)
- toast_button.pack(pady=10)
- # Run the Tkinter event loop
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement