Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_all-in-one.py
- from tkinter import *
- import tkinter.ttk as ttk
- from tkinter.colorchooser import *
- from tkinter import messagebox
- my_font = "Arial 24 bold italic"
- def create_menu():
- menu_bar = Menu(root)
- filemenu = Menu(menu_bar, tearoff=0)
- submenu = Menu(filemenu, tearoff=0)
- submenu.add_command(label="Browse Folders", command=None)
- submenu.add_command(label="File 1.txt", command=None)
- submenu.add_command(label="File 2.txt", command=None)
- submenu.add_command(label="File 3.txt", command=None)
- filemenu.add_cascade(label="Open", menu=submenu)
- filemenu.add_command(label ="Save")
- filemenu.add_separator()
- filemenu.add_command(label ="Exit")
- menu_bar.add_cascade(label ='File', menu=filemenu)
- editmenu = Menu(menu_bar, tearoff=0)
- editmenu.add_command(label ="Cut")
- editmenu.add_command(label ="Copy")
- editmenu.add_command(label ="Paste")
- menu_bar.add_cascade(label ='Edit', menu=editmenu)
- root.config(menu=menu_bar)
- def create_label(frame):
- label = Label(frame, text="This Is A Label.", font=my_font, fg='#ff0000')
- label.pack()
- def create_button(frame):
- button = Button(frame, text="Click Me!", command=lambda: create_message_window(frame))
- button.pack()
- def create_entry(frame, mystring="This Is An Entry."):
- entry_value = StringVar()
- entry_value.set(mystring)
- entry = Entry(frame, text=entry_value)
- entry.pack()
- def create_checkbutton(frame, mystring="Check Me!"):
- checkbutton = Checkbutton(frame, text=mystring)
- checkbutton.pack()
- def create_radiobutton(frame):
- radiobutton1 = Radiobutton(frame, text="Option 1", value=1, variable=selected_value)
- radiobutton2 = Radiobutton(frame, text="Option 2", value=2, variable=selected_value)
- radiobutton3 = Radiobutton(frame, text="Option 3", value=3, variable=selected_value)
- radiobutton1.pack()
- radiobutton2.pack()
- radiobutton3.pack()
- def create_scrollbar_and_listbox(frame, listbox_items=["Item " + str(i + 1) for i in range(50)], preselect=27):
- preselect -= 1
- scrollbar = Scrollbar(frame)
- listbox = Listbox(frame, yscrollcommand=scrollbar.set)
- scrollbar.pack(side=RIGHT, fill=Y)
- scrollbar.config(command=listbox.yview)
- for item in listbox_items:
- listbox.insert("end", item)
- listbox.pack(fill=BOTH)
- listbox.activate(preselect) # activate 27th item (0-indexed)
- listbox.selection_set(preselect, last=None) # select 27th item (0-indexed)
- listbox.yview_scroll(preselect, "units")
- def create_scale(frame, start=0, end=100, preselect=72):
- scale = Scale(frame, from_=start, to=end)
- scale.set(preselect)
- scale.pack()
- def create_spinbox(frame, spinbox_values=(1, 2, 3, 4, 5, 6, 7, 8, 9), preselect=6):
- spinbox = ttk.Spinbox(frame, values=spinbox_values)
- spinbox.set(preselect) # requires ttk to work
- spinbox.pack()
- def create_combobox(frame):
- combo = ttk.Combobox(frame, values=options)
- combo.pack()
- def create_message_window(e):
- messagebox.showinfo("Title", "This Is A Message.")
- def get_color():
- color = askcolor(title="Choose Color")
- messagebox.showinfo("Color Selected", str(color).split()[-1][:-1])
- root = Tk()
- root.title("All-In-One Tkinter")
- options = ["Option 1", "Option 2", "Option 3"]
- option_var = StringVar(value=options[0])
- frame1 = Frame(bd=5)
- frame1.pack(side=LEFT)
- frame2 = Frame(bd=5)
- frame2.pack(side=RIGHT)
- # Radiobutton (can only be pre-selected in global?)
- selected_value = IntVar()
- selected_value.set(2)
- create_menu()
- create_combobox(frame1)
- create_radiobutton(frame1)
- create_scrollbar_and_listbox(frame1)
- create_label(frame2)
- create_button(frame2)
- create_entry(frame2)
- create_checkbutton(frame2)
- create_scale(frame2)
- create_spinbox(frame2)
- color_button = Button(frame2, text="Choose Color", command=get_color)
- color_button.pack()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement