button placement.

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
jan.b
User
Beiträge: 160
Registriert: Mittwoch 9. August 2017, 17:12

Re: button placement.

Beitragvon jan.b » Montag 29. Januar 2018, 17:45

Damit war das positionieren eines buttons nach ausgewählten Koordinaten innerhalb eines canvas Objektes gemeint.
Benutzeravatar
wuf
User
Beiträge: 1368
Registriert: Sonntag 8. Juni 2003, 09:50

Re: button placement.

Beitragvon wuf » Montag 29. Januar 2018, 20:54

jan.b hat geschrieben:Damit war das positionieren eines buttons nach ausgewählten Koordinaten innerhalb eines canvas Objektes gemeint.

OK! Hier die Variante mit der Platzierung auf einer Canvas:
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import tkinter as tk
  5.  
  6. APP_TITLE = "Button placing on canvas"
  7. APP_XPOS = 100
  8. APP_YPOS = 100
  9. APP_WIDTH = 600
  10. APP_HEIGHT = 200
  11.  
  12. ANCHORS = ['center', 'n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw']
  13. FONT = ('Helevetica', 14, 'bold')
  14.  
  15.        
  16. class Application(tk.Tk):
  17.  
  18.     def __init__(self):
  19.         tk.Tk.__init__(self)
  20.         self.title(APP_TITLE)
  21.        
  22.     def build(self):
  23.         self.protocol("WM_DELETE_WINDOW", self.close_app)
  24.         self.geometry("+{}+{}".format(APP_XPOS, APP_YPOS))
  25.         self.geometry("{}x{}".format(APP_WIDTH, APP_HEIGHT))
  26.         self.option_add("*Button.highlightThickness", 0)
  27.        
  28.         self.canvas = tk.Canvas(self, bg='#FFFF00')
  29.         self.canvas.pack(fill='both', expand=True)
  30.         self.canvas.update_idletasks()
  31.        
  32.         self.pos_var = tk.StringVar()
  33.         self.button = tk.Button(None, textvariable=self.pos_var, width=18,
  34.             height=2, font=FONT)
  35.         self.button.pack(padx=10, pady=10)
  36.        
  37.         self.button_width = self.button.winfo_reqwidth()
  38.         self.button_height = self.button.winfo_reqheight()
  39.        
  40.         self.button_xpos = 0
  41.         self.button_ypos = 0
  42.         self.button_window = self.canvas.create_window(
  43.             self.button_xpos,
  44.             self.button_ypos,
  45.             window=self.button, anchor='nw', tag='button')
  46.        
  47.         self.place_the_button()
  48.        
  49.     def place_the_button(self, pos_index=0):
  50.         canvas_width = self.canvas.winfo_width()
  51.         canvas_height = self.canvas.winfo_height()
  52.         print(self.button_width, self.button_height, canvas_width, canvas_height)
  53.         anchor = ANCHORS[pos_index]
  54.         if anchor == 'center':
  55.             xpos = (canvas_width-self.button_width) / 2
  56.             ypos = (canvas_height-self.button_height) / 2
  57.         elif anchor == 'n':
  58.             xpos = (canvas_width-self.button_width) /2
  59.             ypos = 0
  60.         elif anchor == 'ne':
  61.             xpos = (canvas_width-self.button_width)
  62.             ypos = 0
  63.         elif anchor == 'e':
  64.             xpos = (canvas_width-self.button_width)
  65.             ypos = (canvas_height-self.button_height) / 2
  66.         elif anchor == 'se':
  67.             xpos = (canvas_width-self.button_width)
  68.             ypos = (canvas_height-self.button_height)
  69.         elif anchor == 's':
  70.             xpos = (canvas_width-self.button_width) / 2
  71.             ypos = (canvas_height-self.button_height)
  72.         elif anchor == 'sw':
  73.             xpos = 0
  74.             ypos = (canvas_height-self.button_height)
  75.         elif anchor == 'w':
  76.             xpos = 0
  77.             ypos = (canvas_height-self.button_height) / 2
  78.         elif anchor == 'nw':
  79.             xpos = 0
  80.             ypos = 0
  81.            
  82.         self.pos_var.set("Position:\n'{}'".format(anchor))    
  83.         self.canvas.coords('button', xpos, ypos)
  84.         pos_index += 1
  85.         if pos_index == len(ANCHORS):
  86.             pos_index = 0
  87.            
  88.         self.after(2000,self.place_the_button, pos_index)
  89.        
  90.     def close_app(self):
  91.         # Here do something before apps shutdown
  92.         print("Good Bye!")
  93.         self.destroy()
  94.  
  95.  
  96. def main():        
  97.     app = Application()
  98.     app.build()
  99.     app.mainloop()
  100.  
  101. if __name__ == '__main__':
  102.     main()
Gruss wuf :wink:
Take it easy Mates!
jan.b
User
Beiträge: 160
Registriert: Mittwoch 9. August 2017, 17:12

Re: button placement.

Beitragvon jan.b » Montag 29. Januar 2018, 22:07

100 zeilen? ganz schön viel. danke für deine ausführliche hilfe. bis ich das ganz verstanden habe, muss ich wohl noch ein bisschen rum googeln, aber wenigstens die Basis habe ich jetzt. :D
Benutzeravatar
wuf
User
Beiträge: 1368
Registriert: Sonntag 8. Juni 2003, 09:50

Re: button placement.

Beitragvon wuf » Dienstag 30. Januar 2018, 07:49

Hi jan.b

Hier noch eine Variante mit wesentlich weniger Zeilen:
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import tkinter as tk
  5.  
  6.        
  7. class Application(tk.Tk):
  8.  
  9.     def __init__(self, title):
  10.         tk.Tk.__init__(self)
  11.         self.title(title)
  12.        
  13.         self.geometry("300x300")
  14.        
  15.         self.canvas = tk.Canvas(self, bg='#FFFF00')
  16.         self.canvas.pack(fill='both', expand=True)
  17.         self.canvas.update_idletasks()
  18.        
  19.         self.pos_var = tk.StringVar()
  20.         self.button = tk.Button(None, text="Button-Widget", width=10)
  21.         self.button.pack()
  22.        
  23.         # Alte Button Position        
  24.         xpos = 0
  25.         ypos = 0
  26.         self.button_window = self.canvas.create_window(xpos, ypos,
  27.             window=self.button, anchor='nw', tag='button')
  28.  
  29.         # Neue Button Position
  30.         xpos = 150
  31.         ypos = 150
  32.         self.canvas.coords('button', xpos, ypos)
  33.  
  34.  
  35. Application("Button placing on canvas").mainloop()
Gruss wuf :wink:
Take it easy Mates!
jan.b
User
Beiträge: 160
Registriert: Mittwoch 9. August 2017, 17:12

Re: button placement.

Beitragvon jan.b » Dienstag 30. Januar 2018, 19:03

danke. ehrlich gesagt ist das sehr viel hilfreicher und motiviert mich dazu das ganze auch richtig durchzugehen. :D

Wer ist online?

Mitglieder in diesem Forum: Bing [Bot]