Hi Dajosef & Flamez
Hier noch ein bisschen code zu den Anregungen von snafu.
Alle Teile in einer Datei:
Code: Alles auswählen
#!/usr/bin/env python
# coding: UTF-8
import sys, os
from functools import partial
try:
#~~ For Python 2.x
import Tkinter as tk
except ImportError:
#~~ For Python 3.x
import tkinter as tk
APP_TITLE = "MVC-Ansatz"
APP_XPOS = 100
APP_YPOS = 100
APP_WIDTH = 400
APP_HEIGHT = 300
APP_BORDER = 10
TEXT_WIN_HEIGHT = 10
TEXT_WIN_WIDTH = 50
STORE_PATH = os.getcwd()
DATA_FILE = "MyData.txt"
class AppModel(object):
"""Model, Logik, Geschäftslogik usw..."""
def __init__(self):
pass
def save_data(self, data=""):
with open(os.path.join(STORE_PATH, DATA_FILE), 'w') as output_file:
output_file.write(data.encode("utf-8"))
def read_data(self):
with open(os.path.join(STORE_PATH, DATA_FILE), 'r') as input_file:
return input_file.read()
class AppGui(tk.Frame):
def __init__(self, app, title=APP_TITLE):
self.app = app
self.app_win = tk.Tk()
self.app_win.title(APP_TITLE)
self.app_win.geometry("+{}+{}".format(APP_XPOS, APP_YPOS))
self.app_win.protocol("WM_DELETE_WINDOW", self.close)
tk.Frame.__init__(self, self.app_win)
self.sub_frame = tk.Frame(self)
self.sub_frame.pack()
self.text = tk.Text(self.sub_frame, height=TEXT_WIN_HEIGHT,
width=TEXT_WIN_WIDTH, highlightthickness=0)
self.text.pack(side=tk.LEFT)
self.button_frame = tk.Frame(self.sub_frame)
self.button_frame.pack(side=tk.LEFT, padx=(4, 0), pady=4)
self.Button_start = tk.Button(self.button_frame, text="Read Data",
command=self.app.read_data)
self.Button_start.pack(fill=tk.X)
self.Button_pause = tk.Button(self.button_frame, text="Save Data",
command=self.app.save_data)
self.Button_pause.pack(fill=tk.X)
self.Button_ende = tk.Button(self.button_frame, text="Close",
command=self.close)
self.Button_ende.pack(fill=tk.X)
self.label = tk.Label(self, text="MyLabel")
self.label.pack(anchor=tk.W, pady=(4, 0))
self.pack(expand=True, padx=APP_BORDER, pady=APP_BORDER)
def update_text(self, new_text):
self.text.delete(1.0, tk.END)
self.text.insert(tk.END, new_text )
def get_text(self):
return self.text.get(1.0, tk.END)
def label_aendern(self, new_text):
self.label.config(text=new_text)
def run(self):
self.app_win.mainloop()
def close(self):
print("GUI-Shutdown")
self.app_win.destroy()
self.app.close(args=None)
class App(object):
def __init__(self, title=APP_TITLE):
self.app_model = AppModel()
self.app_gui = AppGui(self)
self.app_gui.run()
def save_data(self):
self.app_model.save_data(self.app_gui.get_text())
def read_data(self):
data = self.app_model.read_data()
self.app_gui.update_text(data)
def close(self, args):
print("Application-Shutdown")
sys.exit()
App()
Hier alles aufgeteilt in drei Dateien
Teil-1 Datei app_model.py:
Code: Alles auswählen
#!/usr/bin/env python
# coding: UTF-8
import os
STORE_PATH = os.getcwd()
DATA_FILE = "MyData.txt"
class AppModel(object):
"""Model, Logik, Geschäftslogik usw..."""
def __init__(self):
pass
def save_data(self, data=""):
with open(os.path.join(STORE_PATH, DATA_FILE), 'w') as output_file:
output_file.write(data.encode("utf-8"))
def read_data(self):
with open(os.path.join(STORE_PATH, DATA_FILE), 'r') as input_file:
return input_file.read()
Teil-2 Datei app_gui.py:
Code: Alles auswählen
#!/usr/bin/env python
# coding: UTF-8
import sys
from functools import partial
try:
#~~ For Python 2.x
import Tkinter as tk
except ImportError:
#~~ For Python 3.x
import tkinter as tk
APP_TITLE = "MVC-Ansatz"
APP_XPOS = 100
APP_YPOS = 100
APP_WIDTH = 400
APP_HEIGHT = 300
APP_BORDER = 10
TEXT_WIN_HEIGHT = 10
TEXT_WIN_WIDTH = 50
class AppGui(tk.Frame):
def __init__(self, app, title=APP_TITLE):
self.app = app
self.app_win = tk.Tk()
self.app_win.title(APP_TITLE)
self.app_win.geometry("+{}+{}".format(APP_XPOS, APP_YPOS))
self.app_win.protocol("WM_DELETE_WINDOW", self.close)
tk.Frame.__init__(self, self.app_win)
self.sub_frame = tk.Frame(self)
self.sub_frame.pack()
self.text = tk.Text(self.sub_frame, height=TEXT_WIN_HEIGHT,
width=TEXT_WIN_WIDTH, highlightthickness=0)
self.text.pack(side=tk.LEFT)
self.button_frame = tk.Frame(self.sub_frame)
self.button_frame.pack(side=tk.LEFT, padx=(4, 0), pady=4)
self.Button_start = tk.Button(self.button_frame, text="Read Data",
command=self.app.read_data)
self.Button_start.pack(fill=tk.X)
self.Button_pause = tk.Button(self.button_frame, text="Save Data",
command=self.app.save_data)
self.Button_pause.pack(fill=tk.X)
self.Button_ende = tk.Button(self.button_frame, text="Close",
command=self.close)
self.Button_ende.pack(fill=tk.X)
self.label = tk.Label(self, text="MyLabel")
self.label.pack(anchor=tk.W, pady=(4, 0))
self.pack(expand=True, padx=APP_BORDER, pady=APP_BORDER)
def update_text(self, new_text):
self.text.delete(1.0, tk.END)
self.text.insert(tk.END, new_text )
def get_text(self):
return self.text.get(1.0, tk.END)
def label_aendern(self, new_text):
self.label.config(text=new_text)
def run(self):
self.app_win.mainloop()
def close(self):
print("GUI-Shutdown")
self.app_win.destroy()
self.app.close(args=None)
Teil-3 app.py:
Code: Alles auswählen
#!/usr/bin/env python
# coding: UTF-8
import sys, os
from app_model import AppModel
from app_gui import AppGui
class App(object):
def __init__(self):
self.app_model = AppModel()
self.app_gui = AppGui(self)
self.app_gui.run()
def save_data(self):
self.app_model.save_data(self.app_gui.get_text())
def read_data(self):
data = self.app_model.read_data()
self.app_gui.update_text(data)
def close(self, args):
print("Application-Shutdown")
sys.exit()
App()
Startskript ist
app.py
Gruss wuf
