Advertisement
JDpaste

Python GUI guizero: ListBox and Picture

Sep 26th, 2019
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.85 KB | None | 0 0
  1. # This Python code is a demo of the GUI guizero (with PushButton trigger)
  2. # guizero works with tkinter but simpler with extra widgets eg. Slider
  3. # https://lawsie.github.io/guizero/slider/
  4. # guizero - Hero name generator
  5. from guizero import App, Text, CheckBox, Combo, PushButton, TextBox, ListBox, Picture
  6. app = App(title="Hero-o-matic", width=500, height=600)
  7.  
  8. # Function definitions for your events go here.
  9. def make_hero_name(): # triggered by PushButton :'Make me a hero'
  10.     adjective = bgp_adjective.value
  11.     colour = txt_colour.value
  12.     animal = cmb_animal.value
  13.     title = listbox.value
  14.     if (title == ""):
  15.         title = "The"
  16.        
  17.     # is Super selected ?
  18.     if (cbSuper.value <= 0): # not checked
  19.         honour = ""
  20.     else:
  21.         honour = "Super "
  22.    
  23.     hero = title + " " + honour + adjective + " " + colour + " " + animal
  24.     lbl_output.value = "You are... " + hero + "."
  25.  
  26. def set_dark_mode(): # if CheckBox selected, use white on black styling otherwise use colour !
  27.     # is Dark selected ?
  28.     if (cbDark.value <= 0): # not checked .. apply chosen colour styling
  29.         # App ..
  30.         app.bg = "#DDDD33"
  31.         app.text_color = "#000000"
  32.        
  33.         h1.text_color="#CC3333" # main Heading
  34.        
  35.         # Make me a hero: PushButton
  36.         btn_make_name.bg = "#CC3333"
  37.         btn_make_name.text_color = "#FFFFFF"
  38.     else: # Dark mode request
  39.         app.bg = "#000000"
  40.         app.text_color = "#FFFFFF"
  41.    
  42. # -- GUI widgets .. starting with a main heading Picture & Text ------------------------
  43. picture = Picture(app, image="images/header_img001.png")
  44. h1 = Text(app, text="Hero-o-matic name builder", size=26, font="Times New Roman")
  45.  
  46. cbDark  = CheckBox(app, text="DARK mode", command= set_dark_mode )
  47. cbSuper = CheckBox(app, text="Super")
  48. messageListBox = Text(app, text="Choose a title ..")
  49. listbox = ListBox( app, height= 70, width= 120,items=["","Ms.","Miss","Mrs.","Mr.","Lord","Lady","Right Honourable"], scrollbar= True, selected= "")
  50.  
  51. message1 = Text(app, text="Choose an adjective")
  52. bgp_adjective = Combo(app, options=["Amazing", "Bonny", "Charming", "Delightful", "Vundebar"], selected="Amazing", width=20)
  53.  
  54. message2 = Text(app, text="Enter a colour?")
  55. txt_colour = TextBox(app)
  56.  
  57. message3 = Text(app, text="Pick an animal")
  58. cmb_animal = Combo(app, options=["Aardvark", "Badger", "Cat", "Dog", "Dolphin", "Velociraptor"], selected="Aardvark", width=20)
  59.  
  60. btn_make_name = PushButton(app, text='Make me a hero', command= make_hero_name ) # event handler linked here
  61. lbl_output = Text(app, text="A hero name will appear here")
  62.  
  63. # -- Set STYLING & other properties -------------------------------------------
  64. # Sizes ..
  65. btn_make_name.text_size = 16  # Make me a hero: PushButton
  66.  
  67. set_dark_mode() # initial colour styling
  68.  
  69. app.display()   # Show the GUI on the screen, start listening to events.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement