button placement.
OK! Hier die Variante mit der Platzierung auf einer Canvas:jan.b hat geschrieben:Damit war das positionieren eines buttons nach ausgewählten Koordinaten innerhalb eines canvas Objektes gemeint.
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tkinter as tk
APP_TITLE = "Button placing on canvas"
APP_XPOS = 100
APP_YPOS = 100
APP_WIDTH = 600
APP_HEIGHT = 200
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)
self.canvas = tk.Canvas(self, bg='#FFFF00')
self.canvas.pack(fill='both', expand=True)
self.canvas.update_idletasks()
self.pos_var = tk.StringVar()
self.button = tk.Button(None, textvariable=self.pos_var, width=18,
height=2, font=FONT)
self.button.pack(padx=10, pady=10)
self.button_width = self.button.winfo_reqwidth()
self.button_height = self.button.winfo_reqheight()
self.button_xpos = 0
self.button_ypos = 0
self.button_window = self.canvas.create_window(
self.button_xpos,
self.button_ypos,
window=self.button, anchor='nw', tag='button')
self.place_the_button()
def place_the_button(self, pos_index=0):
canvas_width = self.canvas.winfo_width()
canvas_height = self.canvas.winfo_height()
print(self.button_width, self.button_height, canvas_width, canvas_height)
anchor = ANCHORS[pos_index]
if anchor == 'center':
xpos = (canvas_width-self.button_width) / 2
ypos = (canvas_height-self.button_height) / 2
elif anchor == 'n':
xpos = (canvas_width-self.button_width) /2
ypos = 0
elif anchor == 'ne':
xpos = (canvas_width-self.button_width)
ypos = 0
elif anchor == 'e':
xpos = (canvas_width-self.button_width)
ypos = (canvas_height-self.button_height) / 2
elif anchor == 'se':
xpos = (canvas_width-self.button_width)
ypos = (canvas_height-self.button_height)
elif anchor == 's':
xpos = (canvas_width-self.button_width) / 2
ypos = (canvas_height-self.button_height)
elif anchor == 'sw':
xpos = 0
ypos = (canvas_height-self.button_height)
elif anchor == 'w':
xpos = 0
ypos = (canvas_height-self.button_height) / 2
elif anchor == 'nw':
xpos = 0
ypos = 0
self.pos_var.set("Position:\n'{}'".format(anchor))
self.canvas.coords('button', xpos, ypos)
pos_index += 1
if pos_index == len(ANCHORS):
pos_index = 0
self.after(2000,self.place_the_button, pos_index)
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()
Take it easy Mates!
Hi jan.b
Hier noch eine Variante mit wesentlich weniger Zeilen:Gruss wuf
Hier noch eine Variante mit wesentlich weniger Zeilen:
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tkinter as tk
class Application(tk.Tk):
def __init__(self, title):
tk.Tk.__init__(self)
self.title(title)
self.geometry("300x300")
self.canvas = tk.Canvas(self, bg='#FFFF00')
self.canvas.pack(fill='both', expand=True)
self.canvas.update_idletasks()
self.pos_var = tk.StringVar()
self.button = tk.Button(None, text="Button-Widget", width=10)
self.button.pack()
# Alte Button Position
xpos = 0
ypos = 0
self.button_window = self.canvas.create_window(xpos, ypos,
window=self.button, anchor='nw', tag='button')
# Neue Button Position
xpos = 150
ypos = 150
self.canvas.coords('button', xpos, ypos)
Application("Button placing on canvas").mainloop()
Take it easy Mates!