Created
November 8, 2012 18:05
-
-
Save anonymous/4040490 to your computer and use it in GitHub Desktop.
Tkinter, Listbox-Auswahl mit Übergabe an Entry-Liste
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 os | |
import csv | |
import codecs | |
import tkinter as tk | |
CONFIG = {'width' : 0, | |
'font': ('NimbusSansL', 14), | |
'font_color': 'black', | |
'back_ground' : 'orange', | |
'select_mode': 'single'} | |
class ListGui(object): | |
def __init__(self, controller, items, items2, main_title, sub_title, | |
conf): | |
self.root = tk.Tk() | |
self.main_title = main_title | |
self.sub_title = sub_title | |
self.root.title(self.main_title) | |
self.controller = controller | |
self.conf = conf | |
xpos = 0 | |
ypos = 0 | |
self.screenx = self.root.winfo_screenwidth() | |
self.screeny = self.root.winfo_screenheight() | |
self.root.geometry("%dx%d+%d+%d" % (self.screenx, self.screeny, | |
xpos, ypos)) | |
self.frame = tk.Frame(self.root) | |
self.frame.pack(side='top', fill='both', expand=True) | |
maxline = (max([len(' '.join(item)) for item in sorted(items)])) | |
minline = (min([len(' '.join(item)) for item in sorted(items)])) | |
step = (maxline - minline) | |
if (maxline - minline) == 0: | |
step = 4 | |
for item in sorted(items): | |
if type(items[item]) == tuple: | |
self.items = ['{0}\t{1}{2}'.format(' '.join(item), | |
((maxline + step - len(' '.join(item))) * ' '), | |
' '.join(items[item])) for item in sorted(items)] | |
if type(items[item]) == str: | |
self.items = ['{0}\t{1}{2}'.format(' '.join(item), | |
((maxline + step - len(' '.join(item))) * ' '), | |
''.join(items[item])) for item in sorted(items)] | |
self.items2 = items2 | |
box_length = len(self.items) | |
max_items = (max([(len(' '.join(item)) + | |
len(' '.join(items[item]))) | |
for item in sorted(items)])) - 5 | |
if max_items > 160: | |
max_items = 160 | |
self.geoxy = set() | |
if self.sub_title != '': | |
self.label_name = tk.Label(self.frame, height=2, | |
width=self.conf['width'], text=self.sub_title, | |
bg=self.conf['back_ground'], font=self.conf['font'], bd=0, | |
highlightthickness=0, fg=self.conf['font_color']) | |
self.label_name.pack(fill='both', expand=False) | |
self.geoxy.add(self.label_name.winfo_reqheight()) | |
self.listbox = tk.Listbox(self.frame, width=max_items, | |
font=self.conf['font'], fg=self.conf['font_color'], | |
selectmode=self.conf['select_mode'], bd=0, highlightthickness=0) | |
self.listbox.pack(pady=30, expand=True) | |
self.listbox.insert(0, *self.items) | |
self.listbox.bind('<<ListboxSelect>>', self.check) | |
self.geoxy.add(self.listbox.winfo_reqheight()) | |
self.root.config() | |
label_names = self.items2 | |
self.scr_frame = self.set_scrolled_entries(self.root, | |
label_names, self.conf) | |
self.scr_frame.pack(expand=True) | |
self.button_frame = tk.Label(self.root, bg=self.conf['back_ground']) | |
self.button_frame.pack(fill='x', ipady=5) | |
tk.Button(self.button_frame, text="Abbrechen", | |
font=self.conf['font'], fg=self.conf['font_color'], | |
highlightthickness=0, command=self.root.destroy,).pack( | |
side='right', padx=4) | |
tk.Button(self.button_frame, text="Übernehmen", | |
font=self.conf['font'], fg=self.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.conf = 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, default_labels, main_title, sub_title): | |
#self.model = Model(listbox_items) | |
#self.model2 = Model(default_labels) | |
self.default_labels = default_labels | |
self.view = ListGui(self, listbox_items, default_labels, | |
main_title, sub_title, 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.default_labels[index], | |
entry_var.get())) | |
entry_data[self.default_labels[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(LISTBOX_ITEMS, DEFAULT_LABELS, MAIN_TITLE, SUB_TITLE).run() | |
#--- MODUL-TEST ---------------------------------------------------------------# | |
if __name__ == '__main__': | |
MAIN_TITLE = 'Anlage Neukunden' | |
SUB_TITLE = 'Auswahl Kundenkreis' | |
# Kundendefinition | |
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'} | |
# Kundenspalten | |
DEFAULT_LABELS = ['Kundenkreis', 'Namen', 'Zusatz', 'Straße, Nr.', | |
'PLZ', 'Ort', 'USt-Ident-Nr.', 'Ansprechpartner', 'Telefon', | |
'Handy', 'Fax', 'Mail','Internetseite'] | |
main() |
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/python3 | |
import tkinter as tk | |
import gui_KundenNeu_01 as app | |
import start_list_names as dat | |
CONFIG = { | |
'width' : 0, | |
'title' : "officeplanet", | |
'start_font': ('NimbusSansL', 80), # Startfenster | |
'big_font': ('NimbusSansL', 14), # Buttons, Texteingabe | |
'txt_font': ('NimbusSansL', 12), # Textausgabe | |
'txt_bground': 'grey', # Texteingabe, Textfenster, Buttons | |
'font_color': 'black', | |
'back_ground' : 'orange' | |
} | |
class View(object): | |
def __init__(self, controller, conf): | |
self.root = tk.Tk() | |
self.root.title(conf['title']) | |
self.controller = controller | |
self.conf = conf | |
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.frame = tk.Frame(self.root) | |
self.frame.pack(side='top', fill='both', expand=True) | |
self.label_button = tk.Label(self.frame, width=18, | |
height=screeny, bg=self.conf['back_ground']) | |
self.label_button.pack(side='left', fill='both') | |
# Lade die benötigten Daten | |
LISTBOX_ITEMS, DEFAULT_LABELS, MAIN_TITLE, SUB_TITLE = dat.kunden() | |
self.button = tk.Button(self.label_button, width=10, | |
text='Kunden', bg=conf['txt_bground'], font=(conf['big_font']), | |
fg=conf['font_color'], | |
command=(lambda: app.Controller(LISTBOX_ITEMS, | |
DEFAULT_LABELS, MAIN_TITLE, SUB_TITLE).run()) | |
).pack(padx=8, ipady=2, pady=19, fill='both') | |
def run(self): | |
self.frame.mainloop() | |
class Controller(object): | |
def __init__(self): | |
self.view = View(self, CONFIG) | |
def run(self): | |
self.view.run() | |
def main(): | |
Controller().run() | |
if __name__ == '__main__': | |
main() |
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/python3 | |
def kunden(): | |
MAIN_TITLE = 'Anlage Neukunden' | |
SUB_TITLE = 'Auswahl Kundenkreis' | |
# Kundendefinition | |
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'} | |
# Kundenspalten | |
DEFAULT_LABELS = ['Kundenkreis', 'Namen', 'Zusatz', 'Straße, Nr.', | |
'PLZ', 'Ort', 'USt-Ident-Nr.', 'Ansprechpartner', 'Telefon', | |
'Handy', 'Fax', 'Mail','Internetseite'] | |
return LISTBOX_ITEMS, DEFAULT_LABELS, MAIN_TITLE, SUB_TITLE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment