Re: Menüauswahl abfragen
Verfasst: Sonntag 24. Dezember 2017, 16:02
Das geht denke ich nicht. Du hast dir da eine Interaktion überlegt für die Menüs nicht gedacht sind. So etwas baut man in einen normalen Dialog.
Seit 2002 Diskussionen rund um die Programmiersprache Python
https://www.python-forum.de/
@wuf: Deine version sieht sehr aufgeräumt aus. Ich habe aber anscheinend noch nicht ganz verstanden, warum Du in die Klassendefinition tk.Frame aufgenommen hast. Ich hätte ja vermutet, dass an der Übergabe app_win auch die Funktion tk.Frame mit dranhängt.wuf hat geschrieben:Hier eine etwas entflochtene und ausführbare Variante des Beitrages:
...
class Application(tk.Frame):
...
Als Basis meiner Experimentierskripte verwende ich folgendes Template um einzelne im Beitrag gesammelte Codefetzen in eine ausführbare Struktur einzufügen. Der Klassenname 'Application' müsste eventuell eher in 'Container' umbenannt werden. Übergeben wird nur das Hauptfenstersobjekt ohne ein Frameobjekt übergeben.suk hat geschrieben:@wuf: Deine version sieht sehr aufgeräumt aus. Ich habe aber anscheinend noch nicht ganz verstanden, warum Du in die Klassendefinition tk.Frame aufgenommen hast. Ich hätte ja vermutet, dass an der Übergabe app_win auch die Funktion tk.Frame mit dranhängt:
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from functools import partial
try:
# Tkinter for Python 2.xx
import Tkinter as tk
except ImportError:
# Tkinter for Python 3.xx
import tkinter as tk
APP_TITLE = "Template"
APP_XPOS = 100
APP_YPOS = 100
APP_WIDTH = 400
APP_HEIGHT = 300
class Application(tk.Frame):
def __init__(self, master):
self.master = master
tk.Frame.__init__(self, master)
def main():
app_win = tk.Tk()
app_win.title(APP_TITLE)
app_win.geometry("+{}+{}".format(APP_XPOS, APP_YPOS))
app_win.geometry("{}x{}".format(APP_WIDTH, APP_HEIGHT))
Application(app_win).pack(fill='both', expand=True)
app_win.mainloop()
if __name__ == '__main__':
main()
Wie würdest du dann das obige Template abändern, dass es deinen Vorstellungen entspricht?__deets__ hat geschrieben:Ich würde die die Vererbung von Frame loswerden. Sie ist unnötig und schlimmstenfalls verwirrend. Sie würde nur dann Sinn ergeben, wenn durch die Ableitung die eigentliche Darstellung innerhalb tkinters verändert würde. So wie man zb in Qt ein QWidget mit überladen der Paint Methode machen kann.
Code: Alles auswählen
class Application(object):
der __init__(self, ...):
self._frame = tk.Frame(....)
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from functools import partial
try:
# Tkinter for Python 2.xx
import Tkinter as tk
except ImportError:
# Tkinter for Python 3.xx
import tkinter as tk
APP_TITLE = "NewTemplate_01"
APP_XPOS = 100
APP_YPOS = 100
APP_WIDTH = 300
APP_HEIGHT = 200
class Application(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.title(APP_TITLE)
self.protocol("WM_DELETE_WINDOW", self.close_app)
self.geometry("+{}+{}".format(APP_XPOS, APP_YPOS))
self.geometry("{}x{}".format(APP_WIDTH, APP_HEIGHT))
self.option_add("*Button.highlightThickness", 0)
# Trial container-01
self.app_container_01 = tk.Frame(self, bg='yellow', bd=0)
self.app_container_01.pack(side='left', fill='both', expand=True)
tk.Button(self.app_container_01, text='Container-01').pack(expand=True)
# Trial container-02
self.app_container_02 = tk.Frame(self, bg='steelblue', bd=0)
self.app_container_02.pack(side='left', fill='both', expand=True)
tk.Button(self.app_container_02, text='Container-02').pack(expand=True)
def close_app(self):
# Here do something before apps shutdown
print("Good Bye!")
self.destroy()
Application().mainloop()
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from functools import partial
try:
# Tkinter for Python 2.xx
import Tkinter as tk
except ImportError:
# Tkinter for Python 3.xx
import tkinter as tk
APP_TITLE = "NewTemplate_01"
APP_XPOS = 100
APP_YPOS = 100
APP_WIDTH = 300
APP_HEIGHT = 200
class Application(object):
def __init__(self, app_win):
self.app_win = app_win
app_win.protocol("WM_DELETE_WINDOW", self.close_app)
# Trial container-01
self.app_container_01 = tk.Frame(app_win, bg='yellow', bd=0)
self.app_container_01.pack(side='left', fill='both', expand=True)
tk.Button(self.app_container_01, text='Container-01').pack(expand=True)
# Trial container-02
self.app_container_02 = tk.Frame(app_win, bg='steelblue', bd=0)
self.app_container_02.pack(side='left', fill='both', expand=True)
tk.Button(self.app_container_02, text='Container-02').pack(expand=True)
def close_app(self):
# Here do something before apps shutdown
print("Good Bye!")
self.app_win.destroy()
def main():
app_win = tk.Tk()
app_win.title(APP_TITLE)
app_win.geometry("+{}+{}".format(APP_XPOS, APP_YPOS))
app_win.geometry("{}x{}".format(APP_WIDTH, APP_HEIGHT))
app_win.option_add("*Button.highlightThickness", 0)
Application(app_win)
app_win.mainloop()
if __name__ == '__main__':
main()
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from functools import partial
try:
# Tkinter for Python 2.xx
import Tkinter as tk
except ImportError:
# Tkinter for Python 3.xx
import tkinter as tk
APP_XPOS = 100
APP_YPOS = 100
APP_WIDTH = 300
APP_HEIGHT = 200
class Application(object):
APP_TITLE = "NewTemplate_01"
def __init__(self):
self.app_win = tk.Tk()
self.app_win.protocol("WM_DELETE_WINDOW", self.close_app)
self.app_win.title(self.APP_TITLE)
self.app_win.geometry("+{}+{}".format(APP_XPOS, APP_YPOS))
self.app_win.geometry("{}x{}".format(APP_WIDTH, APP_HEIGHT))
self.app_win.option_add("*Button.highlightThickness", 0)
self.init_ui()
self.run_app()
def init_ui(self):
# Trial container-01
self.app_container_01 = tk.Frame(self.app_win, bg='yellow', bd=0)
self.app_container_01.pack(side='left', fill='both', expand=True)
tk.Button(self.app_container_01, text='Container-01').pack(expand=True)
# Trial container-02
self.app_container_02 = tk.Frame(self.app_win, bg='steelblue', bd=0)
self.app_container_02.pack(side='left', fill='both', expand=True)
tk.Button(self.app_container_02, text='Container-02').pack(expand=True)
def run_app(self):
self.app_win.mainloop()
def close_app(self):
# Here do something before apps shutdown
print("Good Bye!")
self.app_win.destroy()
def main():
Application()
if __name__ == '__main__':
main()
Code: Alles auswählen
import sys
from PyQt4 import QtGui
class Application(QtGui.QMainWindow):
def __init__(self):
super(Application, self).__init__()
self.setGeometry(20, 20, 300, 200)
self.setWindowTitle("Qt Template")
self.show()
def main():
app = QtGui.QApplication(sys.argv)
application = Application()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from functools import partial
try:
# Tkinter for Python 2.xx
import Tkinter as tk
except ImportError:
# Tkinter for Python 3.xx
import tkinter as tk
APP_TITLE = "Trial Template"
APP_XPOS = 100
APP_YPOS = 100
APP_WIDTH = 300
APP_HEIGHT = 200
class Application(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.title(APP_TITLE)
def build(self):
self.protocol("WM_DELETE_WINDOW", self.close_app)
self.geometry("+{}+{}".format(APP_XPOS, APP_YPOS))
self.geometry("{}x{}".format(APP_WIDTH, APP_HEIGHT))
self.option_add("*Button.highlightThickness", 0)
# Trial container-01
self.app_container_01 = tk.Frame(self, bg='yellow', bd=0)
self.app_container_01.pack(side='left', fill='both', expand=True)
tk.Button(self.app_container_01, text='Container-01').pack(expand=True)
# Trial container-02
self.app_container_02 = tk.Frame(self, bg='steelblue', bd=0)
self.app_container_02.pack(side='left', fill='both', expand=True)
tk.Button(self.app_container_02, text='Container-02').pack(expand=True)
def close_app(self):
# Here do something before apps shutdown
print("Good Bye!")
self.destroy()
def main():
app = Application()
app.build()
app.mainloop()
if __name__ == '__main__':
main()
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from functools import partial
try:
# Tkinter for Python 2.xx
import Tkinter as tk
except ImportError:
# Tkinter for Python 3.xx
import tkinter as tk
APP_TITLE = "Template_01"
APP_XPOS = 100
APP_YPOS = 100
APP_WIDTH = 300
APP_HEIGHT = 200
BUTTON_01 = 'Container-01'
BUTTON_02 = 'Container-02'
BUTTON_03 = 'Close'
class AppModel(object):
def __init__(self):
pass
class AppView(tk.Frame):
def __init__(self, app, **kwargs):
self.app = app
self.app_win = app.app_win
tk.Frame.__init__(self, self.app_win, **kwargs)
def build(self):
self.container_frame = tk.Frame(self)
self.container_frame.pack(fill='both', expand=True)
# Trial container-01
self.app_container_01 = tk.Frame(self.container_frame, bg='yellow',
bd=0)
self.app_container_01.pack(side='left', fill='both', expand=True)
tk.Button(self.app_container_01, text=BUTTON_01,
command=partial(self.app.buttons_callback, BUTTON_01)
).pack(expand=True)
# Trial container-02
self.app_container_02 = tk.Frame(self.container_frame, bg='steelblue',
bd=0)
self.app_container_02.pack(side='left', fill='both', expand=True)
tk.Button(self.app_container_02, text=BUTTON_02,
command=partial(self.app.buttons_callback, BUTTON_02)
).pack(expand=True)
tk.Button(self, text=BUTTON_03, command=partial(
self.app.buttons_callback, BUTTON_03)).pack(pady=2)
class Application(object):
def __init__(self, app_win):
self.app_win = app_win
app_win.protocol("WM_DELETE_WINDOW", self.close_app)
def build(self):
self.model = AppModel()
self.view = AppView(self)
self.view.pack(fill='both', expand=True)
self.view.build()
def buttons_callback(self, button_name, event=None):
print("My name is", button_name)
if button_name == BUTTON_01:
pass
elif button_name == BUTTON_02:
pass
elif button_name == BUTTON_03:
self.close_app()
def close_app(self):
# Here do things before shutdown
print("Good Bye!")
self.app_win.destroy()
def main():
app_win = tk.Tk()
app_win.title(APP_TITLE)
#app_win.geometry("+{}+{}".format(APP_XPOS, APP_YPOS))
#app_win.geometry("{}x{}".format(APP_WIDTH, APP_HEIGHT))
app_win.option_add("*Button.highlightThickness", 0)
app = Application(app_win).build()
app_win.mainloop()
if __name__ == '__main__':
main()