Created
November 8, 2012 11:47
-
-
Save anonymous/4038350 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/python3 | |
def kunden(): | |
main_title = 'Anlage Neukunden' # listbox_items | |
sub_title = 'Auswahl Kundenkreis' # default_labels | |
# Kundendefintion | |
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 | |
def produktdaten(): | |
main_title = 'Produktdaten' # listbox_items | |
sub_title = 'Auswahl Produktdaten' # default_labels | |
# Produktspalten | |
listbox_items = '' # Wird aus Liste übergeben | |
# Produktspalten | |
default_labels = ['Lieferant / Artikel', 'Hersteller', | |
'Herstellernummer', 'VE', 'Inhalt', 'Benennung', 'Beschreibung', | |
'Menge', 'Ek', 'Vk'] | |
return listbox_items, default_labels, main_title, sub_title |
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() | |
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 os | |
import sys | |
import csv | |
import codecs | |
import fileinput | |
import tkinter as tk | |
from tkinter.filedialog import askopenfilename, END | |
from gui_start_moduls import screen_blank, openfile, openfile_search | |
from gui_start_moduls import openfile_intern, openfile_save_intern | |
from filestart import fileopen | |
from gui_list_names import kunden, produktdaten | |
import gui_listbox_work as app | |
# datapool.txt | |
datapool_path = os.path.join(os.path.dirname(__file__), | |
'tmp', 'datapool.txt') | |
# liste.txt | |
artikelgesamt_path = os.path.join(os.path.dirname(__file__), | |
'base_work', 'LISTE_ARTIKELGESAMT.txt') | |
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', | |
'select_mode': 'single' | |
} | |
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) | |
# Frame links | |
self.label_left = tk.Label(self.frame, width=18, height=screeny, | |
bg=self.conf['back_ground']) | |
self.label_left.pack(side='left', fill='both') | |
def left_label(): | |
self.label_button = tk.Label(self.label_left, width=18, | |
height=screeny, bg=self.conf['back_ground']) | |
self.label_button.pack(side='left', fill='both') | |
left_label() | |
def left_button(txt, commando): | |
self.button = tk.Button(self.label_button, width=10, | |
text=txt, bg=conf['txt_bground'], font=(conf['big_font']), | |
fg=conf['font_color'], command=commando).pack(padx=8, | |
ipady=2, pady=19, fill='both') | |
# Frame rechts | |
self.label_right = tk.Label(self.frame, width=18, height=screeny, | |
bg=self.conf['back_ground']) | |
self.label_right.pack(side='right', fill='both') | |
# Frame für Textfenster | |
self.txtLabel = tk.Label(self.frame, width=200, | |
bg=self.conf['back_ground'], fg='brown', text=conf['title'], | |
font=(self.conf['start_font'])) | |
self.txtLabel.pack(anchor='n', expand='YES', fill='both') | |
def window_TXT(): | |
self.txtWindow = tk.Text(self.txtLabel, width=200, | |
bg=conf['txt_bground'], fg=conf['font_color'], | |
font=(conf['txt_font'])) | |
self.txtWindow.pack(anchor='n', expand=True, fill='both') | |
self.name_of_side = conf['title'] | |
def Name_Side(name): | |
self.nameInfo = tk.Label(self.txtLabel, width=200, | |
bg=self.conf['back_ground'], fg='brown', | |
text=name, font=(self.conf['start_font'])) | |
self.nameInfo.pack(anchor='n', expand='YES', fill='both') | |
Name_Side(self.name_of_side) | |
# Frame unten | |
label_down = tk.Label(self.frame, heigh=8, width=200, | |
bg=self.conf['back_ground']) | |
label_down.pack(anchor='s', fill='both') | |
# Frame unten, für Beschriftung,Eingabefeld und Buttons | |
label_down1 = tk.Label(label_down, heigh=2, width=200, | |
bg=self.conf['back_ground']) | |
label_down1.pack(anchor='n') | |
label_down2 = tk.Label(label_down, heigh=3, width=200, | |
bg=self.conf['back_ground']) | |
label_down2.pack(anchor='s') | |
label_down3 = tk.Label(label_down, heigh=3, width=200, | |
bg=self.conf['back_ground']) | |
label_down3.pack(anchor='s') | |
# Text-Beschriftung | |
self.txt = tk.Label(label_down1, text='Artikelnummer', width=20, | |
bg=conf['back_ground'], fg=conf['font_color'], | |
font=(conf['big_font'])) | |
self.txt.pack(anchor='n', side='left', ipady=3, padx=25) | |
self.txt = tk.Label(label_down1, text='Textfeld', width=65, | |
bg=conf['back_ground'], fg=conf['font_color'], | |
font=(conf['big_font'])) | |
self.txt.pack(anchor='n', side='right', ipady=3, padx=25) | |
# Text-Eingabe | |
self.article = tk.Entry(label_down2, width=20, | |
bg=conf['txt_bground'], fg=conf['font_color'], | |
font=(conf['big_font'])) | |
self.article.pack(anchor='s', side='left', ipady=3, padx=25) | |
self.txt_line = tk.Entry(label_down2, width=65, | |
bg=conf['txt_bground'], fg=conf['font_color'], | |
font=(conf['big_font'])) | |
self.txt_line.pack(side='right', ipady=3, padx=25) | |
# Buttons rechts | |
button_right = { | |
'01-Kunden' : lambda: check_Side('01'), | |
'02-Lieferanten' : lambda: check_Side('02'), | |
'03-Aufträge' : lambda: check_Side('03'), | |
'04-Bestellungen' : lambda: check_Side('04'), | |
'05-Rechnungen' : lambda: check_Side('05'), | |
'06-Verwaltung' : lambda: check_Side('06'), | |
'07-Buchhaltung' : lambda: check_Side('07'), | |
'08-EDV' : lambda: check_Side('08'), | |
'09-Produkte' : lambda: check_Side('09'), | |
} | |
for row in sorted(button_right): | |
tk.Button(self.label_right, width=10, | |
text=row.split('-')[1], bg=conf['txt_bground'], | |
font=(conf['big_font']), fg=conf['font_color'], | |
command=button_right[row]).pack(padx=8, ipady=2, pady=19) | |
# Buttons links | |
button_left = { | |
'0101-Neuanlage' : lambda: check_Side('0101'), | |
'0102-Formulare' : lambda: check_Side('0102'), | |
'0103-Registrierung' : lambda: check_Side('0103'), | |
'0104-Konditionen' : lambda: check_Side('0104'), | |
'0105-Bank' : lambda: check_Side('0105'), | |
'0106-Info' : lambda: check_Side('0106'), | |
'0107-Kunde' : lambda: check_Side('0107'), | |
'0108-Angebot' : lambda: check_Side('0108'), | |
'0109-History' : lambda: check_Side('0109'), | |
'0201-Neuanlage' : lambda: check_Side('0201'), | |
'0202-Formulare' : lambda: check_Side('0202'), | |
'0203-Konditionen' : lambda: check_Side('0203'), | |
'0204-Bank' : lambda: check_Side('0204'), | |
'0205-Info' : lambda: check_Side('0205'), | |
'0206-Lieferant' : lambda: check_Side('0206'), | |
'0207-Angebot' : lambda: check_Side('0207'), | |
'0208-History' : lambda: check_Side('0208'), | |
'0801-Open_Data' : lambda: openfile_intern(self.txtWindow), | |
'0802-Save_Data' : lambda: openfile_save_intern(self.txtWindow), | |
} | |
def update_leftSide(): | |
try: | |
self.label_button.destroy() | |
left_label() | |
except AttributeError: | |
pass | |
def update_Name(): | |
try: | |
self.nameInfo.destroy() | |
except AttributeError: | |
pass | |
def update_TXT(): | |
try: | |
self.txtWindow.destroy() | |
except AttributeError: | |
pass | |
def update_group(): | |
update_leftSide() | |
update_Name() | |
update_TXT() | |
def window_name(group): | |
base_name = dict() | |
for row in button_right: | |
base_name[row.split('-')[0]] = row.split('-')[1] | |
if base_name.get(group): | |
self.name_of_side = base_name.get(group) | |
Name_Side(self.name_of_side) | |
def check_Side(group): | |
if len(group.split('-')[0]) == 2: | |
update_group() | |
window_name(group) | |
for subgroup in sorted(button_left): | |
if group != '' and group == subgroup[:2]: | |
index_group = subgroup[:4] | |
left_button(subgroup.split('-')[1], | |
button_left[subgroup]) | |
if group == '01' or group[:2] == '01': | |
if group == '0101': | |
(listbox_items, default_labels, main_title, | |
sub_title) = kunden() | |
app.Controller(listbox_items, default_labels, | |
main_title, sub_title).run() | |
if group == '02': | |
pass | |
if group == '03': | |
pass | |
if group == '04': | |
pass | |
if group == '05': | |
pass | |
if group == '06': | |
pass | |
if group == '07': | |
pass | |
if group == '08': | |
pass | |
if group == '09': | |
self.data = openfile_search(artikelgesamt_path) | |
# Buttons unten | |
button = { | |
'01-Suchen' : lambda: check_base_button('01'), | |
'02-Übernehmen' : lambda: check_base_button('02'), | |
'03-Auswahl' : lambda: check_base_button('03'), | |
'04-Zurück' : lambda: check_base_button('04'), | |
'05-Clean' : lambda: screen_blank(self.txtWindow), | |
} | |
for index, b in enumerate(sorted(button)): | |
tk.Button(label_down3, width=10, text=b.split('-')[1], | |
bg=conf['txt_bground'], font=(conf['big_font']), | |
fg=conf['font_color'], command=button[b]).pack( | |
anchor='w', side='left', padx=34, pady=10) | |
def check_base_button(group): | |
try: | |
if group == '01': | |
update_Name() | |
update_TXT() | |
window_TXT() | |
container = set() | |
for row in self.data: | |
container.add(tuple(row)) | |
self.data.clear() | |
self.data = container | |
self.pool = set() | |
entry0 = self.article.get() | |
entry00 = dict() | |
entry00[entry0.lower()] = entry0 | |
entry1 = self.txt_line.get() | |
screen_blank(self.txtWindow) | |
for row in self.data: | |
if entry00.get(row[8]): | |
self.pool.add(tuple(row)) | |
if entry1: | |
for x in row: | |
if entry1 in x or entry1 in x.lower(): | |
self.pool.add(tuple(row)) | |
for row in self.pool: | |
self.txtWindow.insert(END, '{}\n\n'.format(row)) | |
if group == '02': | |
self.data = self.pool | |
if group == '03': | |
self.pool = dict() | |
for item in sorted(self.data): | |
self.pool[item[7:9]] = item[:9] | |
(listbox_items, default_labels, main_title, | |
sub_title) = produktdaten() | |
app.Controller(self.pool, default_labels, main_title, | |
sub_title).run() | |
if group == '04': | |
self.data = openfile_search(liste_path) | |
except AttributeError: | |
pass | |
# Hauptmenü | |
mainMenu = tk.Menu(self.frame, bg=self.conf['back_ground']) | |
self.root.config(menu=mainMenu) | |
mainmenu = { | |
'01_Datei' : 'Datei', | |
'02_EDV' : 'EDV', | |
'03_Sicherheit' : 'Sicherheit', | |
} | |
submenu = { | |
'011_Öffnen' : ['Öffnen', openfile], | |
'012_Intern_öffnen' : ['Intern öffnen', openfile_intern], | |
'013_Intern_speichern' : ['Intern speichern', openfile_save_intern], | |
'014_Bildschirm_leeren' : ['Bildschirm leeren', screen_blank], | |
'015_Einstellungen' : ['Einstellungen', ''], | |
'016_Beenden' : ['Beenden', self.frame.quit], | |
'020_Daten_Update' : ['Daten-Update', ''], | |
'021_Edit Basedata' : ['Edit Basedata', ''], | |
'031_Backup' : ['Backup', ''], | |
'032_Restore' : ['Restore', ''], | |
} | |
for m in sorted(mainmenu): | |
name = ''.join(m[:2]) | |
name = tk.Menu(mainMenu) | |
mainMenu.add_cascade(label=m[3:], menu=name) | |
for s in sorted(submenu): | |
if m[:2] == s[:2]: | |
name.add_command(label=submenu[s][0], | |
command=submenu[s][1]) | |
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment