Created
October 29, 2012 09:18
-
-
Save anonymous/3972548 to your computer and use it in GitHub Desktop.
Tkinter, Fenstergestaltung für Kundenneuanlage
This file contains 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' : 100, | |
'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'] | |
LISTBOX_ITEMS = {'0' : 'Privatkunde', | |
'1' : 'Kleinbetrieb, bis 1000 € Jahresumsatz', | |
'2' : 'Mittelständischer Betrieb, bis 10.000 € Jahresumsatz', | |
'3' : 'Großbetrieb, bis 25.000 € Jahresumsatz', | |
'4' : 'Konzern, ab 25.000 € Jahresumsatz', | |
'5' : 'Komunaler Träger'} | |
class ListGui(object): | |
def __init__(self, controller, items, conf): | |
self.root = tk.Tk() | |
xpos = 0 | |
ypos = 0 | |
screenx = self.root.winfo_screenwidth() | |
screeny = self.root.winfo_screenheight() | |
self.root.geometry("%dx%d+%d+%d" % (screenx, screeny, xpos, ypos)) | |
self.root.title(conf['title']) | |
self.controller = controller | |
self.conf = conf | |
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', padx=5, pady=5, fill='both', | |
expand=True) | |
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.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.root.config() | |
label_names = DEFAULT_LABELS | |
self.scr_frame = self.set_scrolled_entries(self.root, label_names, screeny) | |
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) | |
def set_scrolled_entries(self, parent, label_names, screeny): | |
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) | |
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, sticky=tk.E) | |
bg = ('#fefefe', '#efefef')[i & 1] | |
entry = tk.Entry(entry_frame, textvariable=var, width=100, | |
font=font, bg=bg) | |
entry.grid(row=i, column=1, 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) | |
canvas.config(scrollregion=(x, y, w,h), width=w, height=h//2) | |
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() | |
class Controller(object): | |
def __init__(self): | |
self.model = Model(LISTBOX_ITEMS) | |
self.model2 = Model(DEFAULT_LABELS) | |
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.model2.items[index], | |
entry_var.get())) | |
entry_data[self.model2.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 | |
self.items2 = items | |
def main(): | |
Controller().run() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment