def __init__(self, theme="arc"): """ :param theme: Theme to show off """ ThemedTk.__init__(self) self.set_theme(theme) # Create widgets self.label = ttk.Label(self, text="This is an example label.") self.dropdown = ttk.OptionMenu(self, tk.StringVar(), "First value") self.entry = ttk.Entry( self, textvariable=tk.StringVar(value="Default entry value.")) self.button = ttk.Button(self, text="Button") self.radio_one = ttk.Radiobutton(self, text="Radio one") self.radio_two = ttk.Radiobutton(self, text="Radio two") self.scroll = ttk.Scrollbar(self, orient=tk.VERTICAL) self.checked = ttk.Checkbutton(self, text="Checked", variable=tk.BooleanVar(value=True)) self.unchecked = ttk.Checkbutton(self, text="Unchecked") self.tree = ttk.Treeview(self, height=4, show=("tree", )) self.setup_tree() self.scale_entry = ScaleEntry(self, from_=0, to=50, orient=tk.HORIZONTAL, compound=tk.RIGHT) self.combo = AutocompleteCombobox( self, completevalues=["something", "something else"]) # Grid widgets self.grid_widgets() # Bind screenshot button self.bind("<F10>", self.screenshot)
def __init__(self, theme="arc"): """ :param theme: Theme to show off """ ThemedTk.__init__(self, fonts=True, themebg=True) self.set_theme(theme) # Create widgets self.notebook = ttk.Notebook(self) self.notebook.add(ttk.Button(self, text="Hello World"), text="Frame One") self.notebook.add(ttk.Button(self, text="Hello Universe"), text="Frame Two") self.menu = tk.Menu(self, tearoff=False) self.sub_menu = tk.Menu(self.menu, tearoff=False) self.sub_menu.add_command(label="Exit", command=self.destroy) self.menu.add_cascade(menu=self.sub_menu, label="General") self.config(menu=self.menu) self.label = ttk.Label(self, text="This is an example label.") self.dropdown = ttk.OptionMenu(self, tk.StringVar(), "First value", "Second Value") self.entry = ttk.Entry( self, textvariable=tk.StringVar(value="Default entry value.")) self.button = ttk.Button(self, text="Button") self.radio_one = ttk.Radiobutton(self, text="Radio one", value=True) self.radio_two = ttk.Radiobutton(self, text="Radio two", value=False) self.scroll = ttk.Scrollbar(self, orient=tk.VERTICAL) self.checked = ttk.Checkbutton(self, text="Checked", variable=tk.BooleanVar(value=True)) self.unchecked = ttk.Checkbutton(self, text="Unchecked") self.tree = ttk.Treeview(self, height=4, show=("tree", "headings")) self.setup_tree() self.scale_entry = ScaleEntry(self, from_=0, to=50, orient=tk.HORIZONTAL, compound=tk.RIGHT) self.combo = AutocompleteCombobox( self, completevalues=["something", "something else"]) self.progress = ttk.Progressbar(self, maximum=100, value=50) # Grid widgets self.grid_widgets() # Bind screenshot button self.bind("<F10>", self.screenshot) self.bind("<F9>", self.screenshot_themes)
def test_scaleentry_limitedintvar(self): var = ScaleEntry.LimitedIntVar(5, 55) var.set(60) self.assertEqual(var.get(), 55) var.set(0) self.assertEqual(var.get(), 5) var.configure(low=10) self.assertEqual(var._low, 10) self.assertEqual(var.get(), 10) var.set(54) var.configure(high=20) self.assertEqual(var._high, 20) self.assertEqual(var.get(), 20) self.assertRaises(TypeError, lambda: var.set('a'))
def test_scaleentry_methods(self): scale = ScaleEntry(self.window, scalewidth=100, entrywidth=4, from_=-10, to=10, orient=tk.VERTICAL, compound=tk.TOP, entryscalepad=10) scale.pack() self.window.update() keys = [ 'borderwidth', 'padding', 'relief', 'width', 'height', 'takefocus', 'cursor', 'style', 'class', 'scalewidth', 'orient', 'entrywidth', 'from', 'to', 'compound', 'entryscalepad' ] keys.sort() self.assertEqual(scale.keys(), keys) self.assertEqual(scale['orient'], tk.VERTICAL) self.assertEqual(scale['scalewidth'], 100) self.assertEqual(scale['entrywidth'], 4) self.assertEqual(scale['from'], -10) self.assertEqual(scale['to'], 10) self.assertEqual(scale['compound'], tk.TOP) self.assertEqual(scale['entryscalepad'], 10) scale.configure({ 'to': 50, 'compound': tk.RIGHT }, padding=2, from_=20, orient='horizontal', entryscalepad=0) scale['entrywidth'] = 5 self.window.update() self.assertEqual(scale.cget('compound'), tk.RIGHT) self.assertEqual(str(scale.cget('padding')[0]), '2') self.assertEqual(scale.cget('entryscalepad'), 0) self.assertEqual(scale.cget('entrywidth'), 5) self.assertEqual(str(scale.cget_scale('orient')), 'horizontal') self.assertEqual(scale.cget_scale('from'), 20) self.assertEqual(scale.cget_scale('to'), 50) self.assertEqual(scale._variable._low, 20) self.assertEqual(scale._variable._high, 50) scale.config({'scalewidth': 50, 'from': -10}, compound=tk.BOTTOM) self.assertEqual(scale.cget_scale('from'), -10) self.assertEqual(scale.cget('scalewidth'), 50) self.assertEqual(scale.cget_scale('length'), 50) with self.assertRaises(ValueError): scale['compound'] = 'topp' with self.assertRaises(ValueError): scale['entryscalepad'] = 'a'
def test_scaleentry_property(self): scale = ScaleEntry(from_=50) self.assertEqual(scale.value, 50)
def test_scaleentry_init(self): scale = ScaleEntry(self.window) scale.pack() self.window.update()
def test_scaleentry_scale(self): scale = ScaleEntry(self.window) scale.pack() self.window.update() scale.config_scale(length=100) self.window.update() self.assertEqual(scale.cget_scale('length'), 100) try: info = scale._scale.grid_info() except TypeError: # bug in some tkinter versions res = str(scale.tk.call('grid', 'info', scale._scale._w)).split("-") info = {} for i in res: if i: key, val = i.strip().split() info[key] = val self.assertEqual(info['sticky'], 'ew') scale.config_scale(orient='vertical') self.window.update() try: info = scale._scale.grid_info() except TypeError: # bug in some tkinter versions res = str(scale.tk.call('grid', 'info', scale._scale._w)).split("-") info = {} for i in res: if i: key, val = i.strip().split() info[key] = val self.assertEqual(info['sticky'], 'ns') scale._variable.set(20) scale._on_scale(None) self.assertEqual(scale._variable.get(), 20)
def test_scaleentry_kwargs(self): self.assertRaises(ValueError, lambda: ScaleEntry(compound="something!")) self.assertRaises(TypeError, lambda: ScaleEntry(self.window, entryscalepad='a'))
def test_scaleentry_entry(self): scale = ScaleEntry(self.window, from_=0, to=50) scale.pack() self.window.update() scale.config_entry(width=10) self.window.update() self.assertEqual(scale.cget_entry('width'), 10) scale._entry.delete(0, tk.END) scale._entry.insert(0, "5") scale._on_entry(None) self.assertEqual(scale._variable.get(), 5) scale._entry.insert(0, "a") scale._on_entry(None) self.assertEqual(scale._variable.get(), 5) scale._entry.insert(0, "") scale._on_entry(None) self.assertEqual(scale._variable.get(), 5)
class Example(ThemedTk): """ Example that is used to create screenshots for new themes. """ def __init__(self): ThemedTk.__init__(self, themebg=True) # Create widgets self.menu = tk.Menu(self, tearoff=False) self.sub_menu = tk.Menu(self.menu, tearoff=False) self.sub_menu.add_command(label="Exit", command=self.destroy) self.menu.add_cascade(menu=self.sub_menu, label="General") self.config(menu=self.menu) self.label = ttk.Label(self, text="This is an example label.") self.dropdown = ttk.OptionMenu(self, tk.StringVar(), "First value", "Second Value") self.entry = ttk.Entry( self, textvariable=tk.StringVar(value="Default entry value.")) self.button = ttk.Button(self, text="Button") self.radio_one = ttk.Radiobutton(self, text="Radio one", value=True) self.radio_two = ttk.Radiobutton(self, text="Radio two", value=False) self.scroll = ttk.Scrollbar(self, orient=tk.VERTICAL) self.checked = ttk.Checkbutton(self, text="Checked", variable=tk.BooleanVar(value=True)) self.unchecked = ttk.Checkbutton(self, text="Unchecked") self.tree = ttk.Treeview(self, height=4, show=("tree", "headings")) self.setup_tree() self.scale_entry = ScaleEntry(self, from_=0, to=50, orient=tk.HORIZONTAL, compound=tk.RIGHT) self.combo = AutocompleteCombobox( self, completevalues=["something", "something else"]) self.progress = ttk.Progressbar(self, maximum=100, value=50) # Grid widgets self.grid_widgets() # Bind screenshot button self.bind("<F10>", self.screenshot) self.bind("<F9>", self.screenshot_themes) def setup_tree(self): """Setup an example Treeview""" self.tree.insert("", tk.END, text="Example 1", iid="1") self.tree.insert("", tk.END, text="Example 2", iid="2") self.tree.insert("2", tk.END, text="Example Child") self.tree.heading("#0", text="Example heading") def grid_widgets(self): """Put widgets in the grid""" sticky = {"sticky": "nswe"} self.label.grid(row=1, column=1, columnspan=2, **sticky) self.dropdown.grid(row=2, column=1, **sticky) self.entry.grid(row=2, column=2, **sticky) self.button.grid(row=3, column=1, columnspan=2, **sticky) self.radio_one.grid(row=4, column=1, **sticky) self.radio_two.grid(row=4, column=2, **sticky) self.checked.grid(row=5, column=1, **sticky) self.unchecked.grid(row=5, column=2, **sticky) self.scroll.grid(row=1, column=3, rowspan=8, padx=5, **sticky) self.tree.grid(row=6, column=1, columnspan=2, **sticky) self.scale_entry.grid(row=7, column=1, columnspan=2, **sticky) self.combo.grid(row=8, column=1, columnspan=2, **sticky) self.progress.grid(row=9, column=1, columnspan=2, padx=5, pady=5, **sticky) def screenshot(self, *args): """Take a screenshot, crop and save""" from mss import mss if not os.path.exists("screenshots"): os.makedirs("screenshots") box = { "top": self.winfo_y(), "left": self.winfo_x(), "width": self.winfo_width(), "height": self.winfo_height() } screenshot = mss().grab(box) screenshot = Image.frombytes("RGB", screenshot.size, screenshot.rgb) screenshot.save("screenshots/{}.png".format( ttk.Style(self).theme_use())) def screenshot_themes(self, *args): """Take a screenshot for all themes available""" from time import sleep for theme in THEMES: example.set_theme(theme) example.update() sleep(0.05) self.screenshot()
def __init__(self, parent, controller): tk.Frame.__init__(self, parent) self.config(pady=100) self.top_frame = tk.Frame(self, background="#f5f5f5", width=700, pady=10, padx=10) self.frame = tk.Frame(self.top_frame, background="#f5f5f5", height=200, width=700, pady=10) self.frame.grid_propagate(False) self.label1 = ttk.Label(self, text="Welcome!", font=LARGE_FONT) self.button1 = ttk.Button( self, text="Visit Page 1", command=lambda: controller.show_frame(PageOne)) self.button2 = ttk.Button( self, text="Visit Page 2", command=lambda: controller.show_frame(PageTwo)) self.button_test = ttk.Button(self.frame, text="Choose File", command=lambda: self.get_file_path()) self.label_welcome = ttk.Label(self.top_frame, text="Welcome!", font=("Helvetica", 16, 'bold')) self.label_point_notes = ttk.Label(self.frame, text="Notes", font=("Helvetica", 14, 'bold')) self.label_point1 = ttk.Label(self.frame, text="-> This is point 1", font=("Helvetica", 14)) self.dropdown = ttk.OptionMenu(self, tk.StringVar(), "First value", "Second Value") self.entry = ttk.Entry( self, textvariable=tk.StringVar(value="Default entry value.")) self.button = ttk.Button(self, text="Button") self.radio_one = ttk.Radiobutton(self, text="Radio one", value=True) self.radio_two = ttk.Radiobutton(self, text="Radio two", value=False) self.scroll = ttk.Scrollbar(self, orient=tk.VERTICAL) self.checked = ttk.Checkbutton(self, text="Checked", variable=tk.BooleanVar(value=True)) self.unchecked = ttk.Checkbutton(self, text="Unchecked") self.tree = ttk.Treeview(self, height=4, show=("tree", "headings")) self.setup_tree() self.scale_entry = ScaleEntry(self, from_=0, to=50, orient=tk.HORIZONTAL, compound=tk.RIGHT) self.combo = AutocompleteCombobox( self, completevalues=["something", "something else"]) self.progress = ttk.Progressbar(self, maximum=100, value=50) # Grid widgets self.set_grid_widgets()
class StartPage(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self, parent) self.config(pady=100) self.top_frame = tk.Frame(self, background="#f5f5f5", width=700, pady=10, padx=10) self.frame = tk.Frame(self.top_frame, background="#f5f5f5", height=200, width=700, pady=10) self.frame.grid_propagate(False) self.label1 = ttk.Label(self, text="Welcome!", font=LARGE_FONT) self.button1 = ttk.Button( self, text="Visit Page 1", command=lambda: controller.show_frame(PageOne)) self.button2 = ttk.Button( self, text="Visit Page 2", command=lambda: controller.show_frame(PageTwo)) self.button_test = ttk.Button(self.frame, text="Choose File", command=lambda: self.get_file_path()) self.label_welcome = ttk.Label(self.top_frame, text="Welcome!", font=("Helvetica", 16, 'bold')) self.label_point_notes = ttk.Label(self.frame, text="Notes", font=("Helvetica", 14, 'bold')) self.label_point1 = ttk.Label(self.frame, text="-> This is point 1", font=("Helvetica", 14)) self.dropdown = ttk.OptionMenu(self, tk.StringVar(), "First value", "Second Value") self.entry = ttk.Entry( self, textvariable=tk.StringVar(value="Default entry value.")) self.button = ttk.Button(self, text="Button") self.radio_one = ttk.Radiobutton(self, text="Radio one", value=True) self.radio_two = ttk.Radiobutton(self, text="Radio two", value=False) self.scroll = ttk.Scrollbar(self, orient=tk.VERTICAL) self.checked = ttk.Checkbutton(self, text="Checked", variable=tk.BooleanVar(value=True)) self.unchecked = ttk.Checkbutton(self, text="Unchecked") self.tree = ttk.Treeview(self, height=4, show=("tree", "headings")) self.setup_tree() self.scale_entry = ScaleEntry(self, from_=0, to=50, orient=tk.HORIZONTAL, compound=tk.RIGHT) self.combo = AutocompleteCombobox( self, completevalues=["something", "something else"]) self.progress = ttk.Progressbar(self, maximum=100, value=50) # Grid widgets self.set_grid_widgets() def get_file_path(self): self.filepath = tk.filedialog.askopenfilename() print(self.filepath) def setup_tree(self): """Setup an example Treeview""" self.tree.insert("", tk.END, text="Example 1", iid="1") self.tree.insert("", tk.END, text="Example 2", iid="2") self.tree.insert("2", tk.END, text="Example Child") self.tree.heading("#0", text="Example heading") def set_grid_widgets(self): """Put widgets in the grid""" sticky = {"sticky": "nswe"} self.label_welcome.grid(row=0, column=0) self.label_point_notes.grid(row=0, column=0) self.label_point1.grid(row=1, column=0) self.button_test.grid(row=2, column=0) self.frame.grid(row=1, column=0) self.top_frame.grid(row=0, column=0) self.top_frame.columnconfigure(0, weight=1) self.columnconfigure(0, weight=1) def grid_widgets(self): """Put widgets in the grid""" sticky = {"sticky": "nswe"} self.button1.grid(row=0, column=1, columnspan=2, **sticky) self.button2.grid(row=1, column=1, columnspan=2, **sticky) self.label1.grid(row=1, column=1, columnspan=2, **sticky) self.dropdown.grid(row=2, column=1, **sticky) self.entry.grid(row=2, column=2, **sticky) self.button.grid(row=3, column=1, columnspan=2, **sticky) self.radio_one.grid(row=4, column=1, **sticky) self.radio_two.grid(row=4, column=2, **sticky) self.checked.grid(row=5, column=1, **sticky) self.unchecked.grid(row=5, column=2, **sticky) self.scroll.grid(row=1, column=3, rowspan=8, padx=5, **sticky) self.tree.grid(row=6, column=1, columnspan=2, **sticky) self.scale_entry.grid(row=7, column=1, columnspan=2, **sticky) self.combo.grid(row=8, column=1, columnspan=2, **sticky) self.progress.grid(row=9, column=1, columnspan=2, padx=5, pady=5, **sticky)
class Example(ThemedTk): """ Example that is used to create screenshots for new themes. """ def __init__(self, theme="arc"): """ :param theme: Theme to show off """ ThemedTk.__init__(self) self.set_theme(theme) # Create widgets self.label = ttk.Label(self, text="This is an example label.") self.dropdown = ttk.OptionMenu(self, tk.StringVar(), "First value") self.entry = ttk.Entry( self, textvariable=tk.StringVar(value="Default entry value.")) self.button = ttk.Button(self, text="Button") self.radio_one = ttk.Radiobutton(self, text="Radio one") self.radio_two = ttk.Radiobutton(self, text="Radio two") self.scroll = ttk.Scrollbar(self, orient=tk.VERTICAL) self.checked = ttk.Checkbutton(self, text="Checked", variable=tk.BooleanVar(value=True)) self.unchecked = ttk.Checkbutton(self, text="Unchecked") self.tree = ttk.Treeview(self, height=4, show=("tree", )) self.setup_tree() self.scale_entry = ScaleEntry(self, from_=0, to=50, orient=tk.HORIZONTAL, compound=tk.RIGHT) self.combo = AutocompleteCombobox( self, completevalues=["something", "something else"]) # Grid widgets self.grid_widgets() # Bind screenshot button self.bind("<F10>", self.screenshot) def setup_tree(self): """Setup an example Treeview""" self.tree.insert("", tk.END, text="Example 1", iid="1") self.tree.insert("", tk.END, text="Example 2", iid="2") self.tree.insert("2", tk.END, text="Example Child") def grid_widgets(self): """Put widgets in the grid""" sticky = {"sticky": "nswe"} self.label.grid(row=1, column=1, columnspan=2, **sticky) self.dropdown.grid(row=2, column=1, **sticky) self.entry.grid(row=2, column=2, **sticky) self.button.grid(row=3, column=1, columnspan=2, **sticky) self.radio_one.grid(row=4, column=1, **sticky) self.radio_two.grid(row=4, column=2, **sticky) self.checked.grid(row=5, column=1, **sticky) self.unchecked.grid(row=5, column=2, **sticky) self.scroll.grid(row=1, column=3, rowspan=8, **sticky, padx=5) self.tree.grid(row=6, column=1, columnspan=2, **sticky) self.scale_entry.grid(row=7, column=1, columnspan=2, **sticky) self.combo.grid(row=8, column=1, columnspan=2, **sticky) def screenshot(self, *args): """Take a screenshot, crop and save""" from mss import mss box = { "top": self.winfo_y(), "left": self.winfo_x(), "width": self.winfo_width(), "height": self.winfo_height() } screenshot = mss().grab(box) screenshot = Image.frombytes("RGB", screenshot.size, screenshot.rgb) screenshot.save("screenshot.png")
# -*- coding: utf-8 -*- # Copyright (c) Juliette Monsel 2018 # For license see LICENSE from ttkwidgets import ScaleEntry import tkinter as tk window = tk.Tk() scaleentry = ScaleEntry(window, scalewidth=200, entrywidth=3, from_=0, to=20) scaleentry.config_entry(justify='center') scaleentry.pack() window.mainloop()