tkinter - Return from inside a function defined inside another function in Python -
i know title isn't clear, i'll try explain in more depth:
i have function f defined inside function g, , want able call f inside g, , make f make g return something. example:
def g(): def f(): return(true) # want g return value when f called f() # cannot replace return(f()) because context of problem extremely weird.
because people have requested context:
class color: def __init__(self, r, g, b): self.r = r self.g = g self.b = b def requestcolor(default=color(0, 0, 0)): colorgui = tk.tk() colorgui.title('select color') rlabel = label(colorgui, text='r: ', anchor=tk.e) glabel = label(colorgui, text='g: ', anchor=tk.e) blabel = label(colorgui, text='b: ', anchor=tk.e) rentry = entry(colorgui) gentry = entry(colorgui) bentry = entry(colorgui) # 1 method want have return statement in def savecommand(): colorgui.destroy() return(color(int(rentry.get()), int(gentry.get()), int(bentry.get()))) # method want have return statement in def quitcommand(): colorgui.destroy() return(color(default.r, default.g, default.b)) # want return color when 1 of these buttons pushed. savebutton = button(colorgui, text='save', command=savecommand) quitbutton = button(colorgui, text='cancel'command=quitcommand) rentry.insert(0, default.r) gentry.insert(0, default.g) bentry.insert(0, default.b) rlabel.grid(row=0, column=0, sticky=tk.e) rentry.grid(row=0, column=1) glabel.grid(row=1, column=0, sticky=tk.e) gentry.grid(row=1, column=1) blabel.grid(row=2, column=0, sticky=tk.e) bentry.grid(row=2, column=1) quitbutton.grid(row=3, column=0, sticky=tk.e+tk.w) savebutton.grid(row=3, column=1, sticky=tk.e+tk.w)
i made method becasue want able along lines of:
newcolor = requestcolor(oldcolor)
in several places in program.
one way solve make button commands modify existing color
, rather trying return new color
.
here working demo based on example code that:
import tkinter tk tkinter import * class color: def __init__(self, r, g, b): self.r = r self.g = g self.b = b def requestcolor(default=color(0, 0, 0)): # modified later save command result = color(default.r, default.g, default.b) colorgui = tk.tk() colorgui.title('select color') rlabel = label(colorgui, text='r: ', anchor=tk.e) glabel = label(colorgui, text='g: ', anchor=tk.e) blabel = label(colorgui, text='b: ', anchor=tk.e) rentry = entry(colorgui) gentry = entry(colorgui) bentry = entry(colorgui) def savecommand(): # set new values result.r = int(rentry.get()) result.g = int(gentry.get()) result.b = int(bentry.get()) colorgui.destroy() def quitcommand(): # keep default values colorgui.destroy() savebutton = button(colorgui, text='save', command=savecommand) quitbutton = button(colorgui, text='cancel', command=quitcommand) rentry.insert(0, default.r) gentry.insert(0, default.g) bentry.insert(0, default.b) rlabel.grid(row=0, column=0, sticky=tk.e) rentry.grid(row=0, column=1) glabel.grid(row=1, column=0, sticky=tk.e) gentry.grid(row=1, column=1) blabel.grid(row=2, column=0, sticky=tk.e) bentry.grid(row=2, column=1) quitbutton.grid(row=3, column=0, sticky=tk.e+tk.w) savebutton.grid(row=3, column=1, sticky=tk.e+tk.w) colorgui.mainloop() return result color = requestcolor() print('color(%s, %s, %s)' % (color.r, color.g, color.b))
Comments
Post a Comment