Advertisement
here2share

# tk_all-in-one.py

Feb 3rd, 2023 (edited)
837
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.84 KB | None | 0 0
  1. # tk_all-in-one.py
  2.  
  3. from tkinter import *
  4. import tkinter.ttk as ttk
  5. from tkinter.colorchooser import *
  6. from tkinter import messagebox
  7.  
  8. my_font = "Arial 24 bold italic"
  9.  
  10. def create_menu():
  11.     menu_bar = Menu(root)
  12.      
  13.     filemenu = Menu(menu_bar, tearoff=0)
  14.     submenu = Menu(filemenu, tearoff=0)
  15.     submenu.add_command(label="Browse Folders", command=None)
  16.     submenu.add_command(label="File 1.txt", command=None)
  17.     submenu.add_command(label="File 2.txt", command=None)
  18.     submenu.add_command(label="File 3.txt", command=None)
  19.     filemenu.add_cascade(label="Open", menu=submenu)
  20.     filemenu.add_command(label ="Save")
  21.     filemenu.add_separator()
  22.     filemenu.add_command(label ="Exit")
  23.     menu_bar.add_cascade(label ='File', menu=filemenu)
  24.  
  25.     editmenu = Menu(menu_bar, tearoff=0)
  26.     editmenu.add_command(label ="Cut")
  27.     editmenu.add_command(label ="Copy")
  28.     editmenu.add_command(label ="Paste")
  29.     menu_bar.add_cascade(label ='Edit', menu=editmenu)
  30.    
  31.     root.config(menu=menu_bar)
  32.  
  33. def create_label(frame):
  34.     label = Label(frame, text="This Is A Label.", font=my_font, fg='#ff0000')
  35.     label.pack()
  36.  
  37. def create_button(frame):
  38.     button = Button(frame, text="Click Me!", command=lambda: create_message_window(frame))
  39.     button.pack()
  40.  
  41. def create_entry(frame, mystring="This Is An Entry."):
  42.     entry_value = StringVar()
  43.     entry_value.set(mystring)
  44.     entry = Entry(frame, text=entry_value)
  45.     entry.pack()
  46.  
  47. def create_checkbutton(frame, mystring="Check Me!"):
  48.     checkbutton = Checkbutton(frame, text=mystring)
  49.     checkbutton.pack()
  50.  
  51. def create_radiobutton(frame):
  52.     radiobutton1 = Radiobutton(frame, text="Option 1", value=1, variable=selected_value)
  53.     radiobutton2 = Radiobutton(frame, text="Option 2", value=2, variable=selected_value)
  54.     radiobutton3 = Radiobutton(frame, text="Option 3", value=3, variable=selected_value)
  55.     radiobutton1.pack()
  56.     radiobutton2.pack()
  57.     radiobutton3.pack()
  58.  
  59. def create_scrollbar_and_listbox(frame, listbox_items=["Item " + str(i + 1) for i in range(50)], preselect=27):
  60.     preselect -= 1
  61.     scrollbar = Scrollbar(frame)
  62.     listbox = Listbox(frame, yscrollcommand=scrollbar.set)
  63.     scrollbar.pack(side=RIGHT, fill=Y)
  64.     scrollbar.config(command=listbox.yview)
  65.     for item in listbox_items:
  66.         listbox.insert("end", item)
  67.     listbox.pack(fill=BOTH)
  68.     listbox.activate(preselect)   # activate 27th item (0-indexed)
  69.     listbox.selection_set(preselect, last=None) # select 27th item (0-indexed)
  70.     listbox.yview_scroll(preselect, "units")
  71.  
  72. def create_scale(frame, start=0, end=100, preselect=72):
  73.     scale = Scale(frame, from_=start, to=end)
  74.     scale.set(preselect)
  75.     scale.pack()
  76.  
  77. def create_spinbox(frame, spinbox_values=(1, 2, 3, 4, 5, 6, 7, 8, 9), preselect=6):
  78.     spinbox = ttk.Spinbox(frame, values=spinbox_values)
  79.     spinbox.set(preselect) # requires ttk to work
  80.     spinbox.pack()
  81.  
  82. def create_combobox(frame):
  83.     combo = ttk.Combobox(frame, values=options)
  84.     combo.pack()
  85.  
  86. def create_message_window(e):
  87.     messagebox.showinfo("Title", "This Is A Message.")
  88.  
  89. def get_color():
  90.     color = askcolor(title="Choose Color")
  91.     messagebox.showinfo("Color Selected", str(color).split()[-1][:-1])
  92.  
  93. root = Tk()
  94. root.title("All-In-One Tkinter")
  95.  
  96. options = ["Option 1", "Option 2", "Option 3"]
  97. option_var = StringVar(value=options[0])
  98.  
  99. frame1 = Frame(bd=5)
  100. frame1.pack(side=LEFT)
  101. frame2 = Frame(bd=5)
  102. frame2.pack(side=RIGHT)
  103.  
  104. # Radiobutton (can only be pre-selected in global?)
  105. selected_value = IntVar()
  106. selected_value.set(2)
  107.  
  108. create_menu()
  109. create_combobox(frame1)
  110. create_radiobutton(frame1)
  111. create_scrollbar_and_listbox(frame1)
  112. create_label(frame2)
  113. create_button(frame2)
  114. create_entry(frame2)
  115. create_checkbutton(frame2)
  116. create_scale(frame2)
  117. create_spinbox(frame2)
  118.  
  119. color_button = Button(frame2, text="Choose Color", command=get_color)
  120. color_button.pack()
  121.  
  122. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement