danke schon mal.
Code: Alles auswählen
tk = Tk()
canvas = Canvas(tk, width=400, height=400)
canvas.pack()
def hi():
print("hi")
btn = Button(tk, width=20, height=30, text="hallo", fg='red', bg='black', command=hi)
btn.pack()
Code: Alles auswählen
Hier ist der 'wuf' schon wieder:__deets__ hat geschrieben:Wuf permanent so etwas postet?
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Tkinter for Python 3.xx
import tkinter as tk
APP_TITLE = "Button placing with pack layouter"
APP_XPOS = 100
APP_YPOS = 100
APP_WIDTH = 600
APP_HEIGHT = 200
SIDES = ['top', 'right', 'bottom', 'left']
ANCHORS = ['center', 'n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw']
FONT = ('Helevetica', 14, 'bold')
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)
frame = tk.Frame(self, bg='#FFFF00')
frame.pack(fill='both', expand=True)
self.pos_var = tk.StringVar()
self.button = tk.Button(frame, textvariable=self.pos_var, width=18,
height=2, font=FONT)
self.button.pack(padx=10, pady=10)
self.place_the_button()
def place_the_button(self, pos_index=0, pos_mode='side'):
if pos_mode == 'side':
side = SIDES[pos_index]
self.pack_with_side(side)
pos_index += 1
if pos_index >= len(SIDES):
pos_index = 0
pos_mode = 'anchor'
elif pos_mode == 'anchor':
anchor = ANCHORS[pos_index]
self.pack_with_anchor(anchor)
pos_index += 1
if pos_index >= len(ANCHORS):
pos_index = 0
pos_mode = 'side'
self.after(2000,self.place_the_button, pos_index, pos_mode)
def pack_with_side(self, side):
# Wichtige voreingestellte Optionen für die Anwendung von 'side' sind:
# 'anchor' = 'center'
# 'expand' = False
print('Side:', self.button.pack_info())
self.button.config(bg='#90EE90')
self.pos_var.set("Position with side:\n'{}'".format(side))
self.button.pack_configure(side=side, anchor='center', expand=False)
def pack_with_anchor(self, anchor):
# Wichtige voreingestellte Optionen für die Anwendung von 'anchor' sind:
# 'expand' = True
print('Anchor:', self.button.pack_info())
self.button.config(bg='#1AB7EA')
self.pos_var.set("Position with anchor:\n'{}'".format(anchor))
self.button.pack_configure(anchor=anchor, 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 -*-
# Tkinter for Python 3.xx
import tkinter as tk
APP_TITLE = "Button placing with pack layouter"
APP_XPOS = 100
APP_YPOS = 100
APP_WIDTH = 600
APP_HEIGHT = 200
SIDES = ['top', 'right', 'bottom', 'left']
ANCHORS = ['center', 'n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw']
FONT = ('Helevetica', 14, 'bold')
class Application(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.title(APP_TITLE)
self.build()
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)
frame = tk.Frame(self, bg='#FFFF00')
frame.pack(fill='both', expand=True)
self.pos_var = tk.StringVar()
self.button = tk.Button(frame, textvariable=self.pos_var, width=18,
height=2, font=FONT)
self.button.pack(padx=10, pady=10)
self.place_the_button()
def place_the_button(self, pos_index=0, pos_mode='side'):
if pos_mode == 'side':
side = SIDES[pos_index]
self.pack_with_side(side)
pos_index += 1
if pos_index >= len(SIDES):
pos_index = 0
pos_mode = 'anchor'
elif pos_mode == 'anchor':
anchor = ANCHORS[pos_index]
self.pack_with_anchor(anchor)
pos_index += 1
if pos_index >= len(ANCHORS):
pos_index = 0
pos_mode = 'side'
self.after(2000,self.place_the_button, pos_index, pos_mode)
def pack_with_side(self, side):
# Wichtige voreingestellte Optionen für die Anwendung von 'side' sind:
# 'anchor' = 'center'
# 'expand' = False
print('Side:', self.button.pack_info())
self.button.config(bg='#90EE90')
self.pos_var.set("Position with side:\n'{}'".format(side))
self.button.pack_configure(side=side, anchor='center', expand=False)
def pack_with_anchor(self, anchor):
# Wichtige voreingestellte Optionen für die Anwendung von 'anchor' sind:
# 'expand' = True
print('Anchor:', self.button.pack_info())
self.button.config(bg='#1AB7EA')
self.pos_var.set("Position with anchor:\n'{}'".format(anchor))
self.button.pack_configure(anchor=anchor, expand=True)
def close_app(self):
# Here do something before apps shutdown
print("Good Bye!")
self.destroy()
Application().mainloop()
Code: Alles auswählen
Application().mainloop()
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Tkinter for Python 3.xx
import tkinter as tk
APP_TITLE = "Button placing with pack layouter"
APP_XPOS = 100
APP_YPOS = 100
APP_WIDTH = 600
APP_HEIGHT = 200
SIDES = ['top', 'right', 'bottom', 'left']
ANCHORS = ['center', 'n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw']
FONT = ('Helevetica', 14, 'bold')
class Application(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.title(APP_TITLE)
self.build()
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)
frame = tk.Frame(self, bg='#FFFF00')
frame.pack(fill='both', expand=True)
self.pos_var = tk.StringVar()
self.button = tk.Button(frame, textvariable=self.pos_var, width=18,
height=2, font=FONT)
self.button.pack(padx=10, pady=10)
self.place_the_button()
def place_the_button(self, pos_index=0, pos_mode='side'):
if pos_mode == 'side':
side = SIDES[pos_index]
self.pack_with_side(side)
pos_index += 1
if pos_index >= len(SIDES):
pos_index = 0
pos_mode = 'anchor'
elif pos_mode == 'anchor':
anchor = ANCHORS[pos_index]
self.pack_with_anchor(anchor)
pos_index += 1
if pos_index >= len(ANCHORS):
pos_index = 0
pos_mode = 'side'
self.after(2000,self.place_the_button, pos_index, pos_mode)
def pack_with_side(self, side):
# Wichtige voreingestellte Optionen für die Anwendung von 'side' sind:
# 'anchor' = 'center'
# 'expand' = False
print('Side:', self.button.pack_info())
self.button.config(bg='#90EE90')
self.pos_var.set("Position with side:\n'{}'".format(side))
self.button.pack_configure(side=side, anchor='center', expand=False)
def pack_with_anchor(self, anchor):
# Wichtige voreingestellte Optionen für die Anwendung von 'anchor' sind:
# 'expand' = True
print('Anchor:', self.button.pack_info())
self.button.config(bg='#1AB7EA')
self.pos_var.set("Position with anchor:\n'{}'".format(anchor))
self.button.pack_configure(anchor=anchor, expand=True)
def close_app(self):
# Here do something before apps shutdown
print("Good Bye!")
self.destroy()
def main():
app = Application()
app.mainloop()
if __name__ == '__main__':
main()
Laufen tut das - nur ist eine Funktion mit 10.000 Zeilen Code nicht wartbar, darin findet du ja nichts mehr wieder... Das sollte man dann in mehr Funktionen runter brechen. Das ist aber nicht tkinter-spezifisch, dass gilt für jeglichen Code.Ist es problematisch wenn sich in dieser Methode weitere 10'000 Zeilen Kode befinden?
Code: Alles auswählen
app = Application()
app.mainloop()
Code: Alles auswählen
def main():
app = Application()
app.build()
app.mainloop()
if __name__ == '__main__':
main()