i've come accross problem putting button objects in list.
i'm not sure if i'm trying achieve can achieved here is..
working sample:
from tkinter import * class main(frame): def __init__(self, master = none): frame.__init__(self, master) self.pack(fill = 'both', expand = true) # general purpose variables ---------- self.isbuttonselected = false self.isbutton_a_selected = false # controll boolean self.isbutton_b_selected = false self.isbutton_c_selected = false self.isbutton_d_selected = false # layers ---------- self.background = frame(self) # buttons ---------- self.button_a = button(self.background, text = "a", bg = 'white', command = self.selectbutton_a) self.button_b = button(self.background, text = "b", bg = 'white', command = self.selectbutton_b) self.button_c = button(self.background, text = "c", bg = 'white', command = self.selectbutton_c) self.button_d = button(self.background, text = "d", bg = 'white', command = self.selectbutton_d) # packs ---------- #layers self.background.pack(fill = 'both', expand = true) #buttons btnpackprefix = {'side' : 'left', 'fill' : 'both', 'expand' : 'true'} self.button_a.pack(btnpackprefix) self.button_b.pack(btnpackprefix) self.button_c.pack(btnpackprefix) self.button_d.pack(btnpackprefix) def selectbutton_a(self): '''the idea here similar radiobuttons. click select. click same deselect. click other deselect old , select new. 1 button can selected @ time. -repeated similar methods.''' if self.isbuttonselected == false: # click-select self.button_a.config(bg = 'green') self.isbuttonselected = true self.isbutton_a_selected = true else: if self.isbutton_a_selected: # click-deselect self.resetbuttons() self.isbuttonselected = false else: # deselect other , select self.resetbuttons() self.button_a.config(bg = 'green') self.isbuttonselected = true self.isbutton_a_selected = true def selectbutton_b(self): if self.isbuttonselected == false: self.button_b.config(bg = 'green') self.isbuttonselected = true self.isbutton_b_selected = true else: if self.isbutton_b_selected: self.resetbuttons() self.isbuttonselected = false else: self.resetbuttons() self.button_b.config(bg = 'green') self.isbuttonselected = true self.isbutton_b_selected = true def selectbutton_c(self): if self.isbuttonselected == false: self.button_c.config(bg = 'green') self.isbuttonselected = true self.isbutton_c_selected = true else: if self.isbutton_c_selected: self.resetbuttons() self.isbuttonselected = false else: self.resetbuttons() self.button_c.config(bg = 'green') self.isbuttonselected = true self.isbutton_c_selected = true def selectbutton_d(self): if self.isbuttonselected == false: self.button_d.config(bg = 'green') self.isbuttonselected = true self.isbutton_d_selected = true else: if self.isbutton_d_selected: self.resetbuttons() self.isbuttonselected = false else: self.resetbuttons() self.button_d.config(bg = 'green') self.isbuttonselected = true self.isbutton_d_selected = true def resetbuttons(self): '''this pretty resets buttons , controll booleans default state''' #a self.button_a.config(bg = 'white') # paint button white self.isbutton_a_selected = false # controll boolean reset #b self.button_b.config(bg = 'white') self.isbutton_b_selected = false #c self.button_c.config(bg = 'white') self.isbutton_c_selected = false #d self.button_d.config(bg = 'white') self.isbutton_d_selected = false def run_application(): app = main() app.master.geometry('200x50') app.mainloop() run_application()
as noticed, code has lots of copy-pasted snippets details changed in each one.
need make shorter somehow.. need 1 function controls these actions
since similar.
i've come this.
not working sample:
from tkinter import * class main(frame): def __init__(self, master = none): frame.__init__(self, master) self.pack(fill = 'both', expand = true) # general purpose variables ---------- self.isbuttonselected = false self.isbutton_a_selected = false # controll boolean self.isbutton_b_selected = false self.isbutton_c_selected = false self.isbutton_d_selected = false # layers ---------- self.background = frame(self) # buttons ---------- self.button_a = button(self.background, text = "a", bg = 'white', command = self.selectbutton(0)) # apparently doesn't work... self.button_b = button(self.background, text = "b", bg = 'white', command = self.selectbutton(1)) self.button_c = button(self.background, text = "c", bg = 'white', command = self.selectbutton(2)) self.button_d = button(self.background, text = "d", bg = 'white', command = self.selectbutton(3)) # packs ---------- #layers self.background.pack(fill = 'both', expand = true) #buttons btnpackprefix = {'side' : 'left', 'fill' : 'both', 'expand' : 'true'} self.button_a.pack(btnpackprefix) self.button_b.pack(btnpackprefix) self.button_c.pack(btnpackprefix) self.button_d.pack(btnpackprefix) def selectbutton(self, = none): '''the idea here similar radiobuttons. click select. click same deselect. click other deselect old , select new. 1 button can selected @ time. -do on objects @ [i]. 'i' given button command''' global buttons, btn_bools buttons = [self.button_a, self.button_b, self.button_c, self.button_d] # list buttons. # raises error. seems button objects cannot listed btn_bools = [self.isbutton_a_selected, self.isbutton_b_selected, self.isbutton_c_selected, self.isbutton_d_selected] # list contoll booleans if self.isbuttonselected == false: # click-select self.buttons[i].config(bg = 'green') self.isbuttonselected = true self.btn_bools[i] = true else: if self.isbutton_a_selected: # click-deselect self.resetbuttons() self.isbuttonselected = false else: # deselect other , select self.resetbuttons() self.buttons[i].config(bg = 'green') self.isbuttonselected = true self.btn_bools[i] = true def resetbuttons(self): '''iterate through buttons/controll booleans , reset default state''' b in range(len(buttons)): buttons[b].config(bg = 'white') bb in range(len(btn_bools)): btn_bools[bb] = false def run_application(): app = main() app.master.geometry('200x50') app.mainloop() run_application()
i tried test similar method 'simple' classes contained int attribute and
worked perfectly!
from error report, listing buttons isn't idea if not impossible.
i did search google lot examples or similar problems, didn't
results.
i searched pointer implementations in python and
got confused. trying see if possible save variable pointing a
button think result same.. list reads it's objects buttons and
raises error.
so, questions are:
1. can done somehow? (shorting first code sample.)
2. listing buttons or widgets in general: if can done, how? if not, why?
3. on second code sample, need button commands contain 'i' argument
function needs. how can typed? seems adding (i) not work.
thanks in advance!
look @ line of code:
self.button_a = button(..., command = self.selectbutton(0))
what happening? self.selectbutton(0)
do? calling function self.selectbutton(0)
, , using result of function value of command
attribute. not intended.
what need instead along lines of this:
self.button_a = button(..., command = lambda: self.selectbutton(0))
this defers call self.selectbutton
until button clicked. allow buttons created, , can assign them list if like:
self.buttons = [self.button_a, self.button_b, self.button_c, self.button_d]
Comments
Post a Comment