Created
November 7, 2012 10:26
-
-
Save anonymous/4030749 to your computer and use it in GitHub Desktop.
Tkinter, Fenstergestaltung für Kundenneuanlage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# For Python3.x | |
import tkinter as tk | |
CONFIG = {'width' : 0, | |
'title' : "Anlage Neukunden", | |
'font': ('NimbusSansL', 14, 'bold'), | |
'font_color': 'black', | |
'back_ground' : 'orange', | |
'select_mode': 'single'} | |
DEFAULT_LABELS = ['Kundenkreis', 'Namen', 'Zusatz', 'Straße, Nr.', 'PLZ', | |
'Ort', 'USt-Ident-Nr.', 'Ansprechpartner', 'Telefon', 'Handy', | |
'Fax', 'Mail','Internetseite'] | |
class ListGui(object): | |
def __init__(self, controller, items, conf): | |
self.root = tk.Tk() | |
self.root.title(conf['title']) | |
self.controller = controller | |
self.conf = conf | |
self.screeny = self.root.winfo_screenheight() | |
self.items = ['{0}\t{1}'.format(item, items[item]) | |
for item in sorted(items)] | |
self.frame = tk.Frame(self.root) | |
self.frame.pack(side='top', fill='both', expand=False) | |
box_length = len(self.items) | |
box_width = max(len(item) for item in self.items) - 5 | |
self.label_name = tk.Label(self.frame, height=2, | |
width=conf['width'], text='Auswahl Kundenkreis', | |
bg=conf['back_ground'], font=conf['font'], bd=0, | |
highlightthickness=0, fg=conf['font_color']) | |
self.label_name.pack(fill='both', expand=False) | |
self.geoxy = set() | |
self.geoxy.add(self.label_name.winfo_reqheight()) | |
self.listbox = tk.Listbox(self.frame, height=box_length, | |
width=box_width, font=conf['font'], fg=conf['font_color'], | |
selectmode=conf['select_mode'], bd=0, highlightthickness=0) | |
self.listbox.pack(pady=30, expand=False) | |
self.listbox.insert(0, *self.items) | |
self.listbox.bind('<<ListboxSelect>>', self.check) | |
self.geoxy.add(self.listbox.winfo_reqheight()) | |
self.root.config() | |
label_names = DEFAULT_LABELS | |
self.scr_frame = self.set_scrolled_entries(self.root, | |
label_names, conf) | |
self.scr_frame.pack(expand=True) | |
self.button_frame = tk.Label(self.root, bg=conf['back_ground']) | |
self.button_frame.pack(fill='x', ipady=5) | |
tk.Button(self.button_frame, text="Abbrechen", | |
font=conf['font'], fg=conf['font_color'], | |
highlightthickness=0, command=self.root.destroy,).pack( | |
side='right', padx=4) | |
tk.Button(self.button_frame, text="Übernehmen", | |
font=conf['font'], fg=conf['font_color'], | |
highlightthickness=0, | |
command=self.controller.collect_entries).pack( | |
side='right', padx=4) | |
self.geoxy.add(self.button_frame.winfo_reqheight()) | |
def set_scrolled_entries(self, parent, label_names, conf): | |
self.entry_vars = [tk.StringVar() for _ in label_names] | |
self.entries = [] | |
scroll_frame = tk.Frame(parent, bg='red') | |
canvas = tk.Canvas(scroll_frame, highlightthickness=0) | |
canvas.grid(row=0, column=0, sticky=tk.NSEW) | |
yscroll = tk.Scrollbar(scroll_frame, orient=tk.VERTICAL, | |
command=canvas.yview) | |
yscroll.grid(row=0, column=1, sticky=tk.NS) | |
canvas.config(yscrollcommand=yscroll.set) | |
scroll_frame.grid_rowconfigure(0, weight=1) | |
self.canvas = canvas | |
self.bind_scroll(yscroll, self.y_scroll) | |
entry_frame = tk.Frame(canvas) | |
font = self.conf['font'] | |
for i, (var, name) in enumerate(zip(self.entry_vars, label_names)): | |
label = tk.Label(entry_frame, text=name, font=font) | |
label.grid(row=i, column=0, padx=10, sticky=tk.E) | |
bg = ('grey', 'grey')[i & 1] | |
entry = tk.Entry(entry_frame, textvariable=var, width=100, | |
font=font, bg=bg) | |
entry.grid(row=i, column=1, pady=2, sticky=tk.W) | |
self.entries.append(entry) | |
canvas.create_window(0, 0, window=entry_frame, anchor=tk.NW) | |
canvas.update_idletasks() | |
x, y, w, h = entry_frame.bbox(tk.ALL) | |
y_value = h | |
if (h * 1.5) > self.screeny: | |
y_value = (self.screeny - (round(sum(self.geoxy) * 2))) | |
canvas.config(scrollregion=(x, y, w, h), width=w, height=y_value) | |
return scroll_frame | |
def check(self, event): | |
indices = list(map(int, self.listbox.curselection())) | |
self.controller.process(self.items[indices[0]]) | |
def run(self): | |
self.root.mainloop() | |
def bind_scroll(self, widget, mode): | |
#~~ Windows | |
widget.bind("<MouseWheel>", mode) | |
#~~ Linux | |
widget.bind("<Button-4>", mode) | |
widget.bind("<Button-5>", mode) | |
def y_scroll(self, event): | |
if event.num == 5 or event.delta < 0: | |
self.canvas.yview_scroll(1, "unit") | |
elif event.num == 4 or event.delta > 0: | |
self.canvas.yview_scroll(-1, "unit") | |
class Controller(object): | |
def __init__(self, LISTBOX_ITEMS): | |
self.model = Model(LISTBOX_ITEMS) | |
self.view = ListGui(self, self.model.items, CONFIG) | |
def process(self, listbox_item): | |
self.view.entry_vars[0].set(listbox_item) | |
print(listbox_item) | |
def collect_entries(self): | |
entry_data = dict() | |
for index, entry_var in enumerate(self.view.entry_vars): | |
print("{} : {}".format(self.model.items[index], | |
entry_var.get())) | |
entry_data[self.model.items[index]] = entry_var.get() | |
print() | |
print(entry_data) | |
def run(self): | |
self.view.run() | |
class Model(object): | |
def __init__(self, items): | |
self.items = items | |
def main(): | |
Controller(LISTBOX_ITEMS).run() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment