Advertisement
pastabowl

pydroid sample

Jan 4th, 2025
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.34 KB | Source Code | 0 0
  1. import tkinter as tk
  2. from jnius import autoclass
  3.  
  4.     # Access Android Toast class
  5. Context = autoclass('android.content.Context')
  6. Toast = autoclass('android.widget.Toast')
  7. String=autoclass("java.lang.String")
  8. Handler=autoclass("android.os.Handler")
  9. Looper=autoclass("android.os.Looper")
  10.  
  11. main_handler=Handler(Looper.getMainLooper())
  12.  
  13. def exit_app():
  14.     root.destroy()
  15.  
  16. def show_toast():
  17.     # Get the text from the entry
  18.     main_handler.post(toast_runnable)
  19.  
  20. def toast_runnable():
  21.     # Get the Android application context
  22.     app_context = autoclass('org.kivy.android.PythonActivity').mActivity
  23.     j_msg=String(entry.get())
  24.     # Show the toast with the message
  25.     Toast.makeText(app_context, j_msg, Toast.LENGTH_SHORT).show()
  26.  
  27. # Create the main window
  28. root = tk.Tk()
  29. root.title("Tkinter with Toast")
  30. root.geometry("300x200")  # Set the window size
  31.  
  32. # Add a label
  33. label = tk.Label(root, text="Enter something:")
  34. label.pack(pady=10)
  35.  
  36. # Add a text entry field
  37. entry = tk.Entry(root, width=25)
  38. entry.pack(pady=5)
  39.  
  40. # Add a button to close the app
  41. exit_button = tk.Button(root, text="Return to Editor", command=exit_app)
  42. exit_button.pack(pady=10)
  43.  
  44. # Add a button to show the toast
  45. toast_button = tk.Button(root, text="Show Toast", command=show_toast)
  46. toast_button.pack(pady=10)
  47.  
  48. # Run the Tkinter event loop
  49. root.mainloop()
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement