Package: dynamic-variables
Change variables during runtime using simple GUI. ## Installationpip3 install dynamic_variablesUsageHere is a minimalist exampleimportdynamic_variablesasdvimporttimedv.add_slider('var_name',0,10)dv.init_gui()whileTrue:print(var_name)time.sleep(0.1)When this program run, a slider appears andvt.var_namechanges
according to this slider.Breakdowndv.add_slidercreates a slider inside the gui and a float variable
namedvar_name. This variable is connected with the slider and
changes with the slider, and can be used throughout the code. Slider is
one of 6 widgets. All widgets and their usage covered in widgets
section.dv.init_gui()Initializes the GUI.WidgetsThere are 6 widgets that you can add to GUI. They allow manipulating
different types of variables.All Widgets(dark theme with dark-blue color)SliderSlider widget allows you to changeintorfloatvariables
easily.add_slider(variable_name,min_value,max_value[,value[,step]])Example:dv.add_slider('slider',0,10,value=5,step=1)If all values areint, the variable will be anint. Otherwise,
it will be afloat.TextText widget allows you to changestrvariables.add_text(variable_name[,value])Example:dv.add_text('text',value='this is a text')DropdownDropdown widget allows you to change your variable to any predetermined
value.add_dropdown(variable_name,list_or_tuple_of_options[,chosen_index])Example:dv.add_text('dropdown',['option 1','option 2',3,4.5],chosen_index=0)BooleanBoolean widget allows you to change yourboolvariableadd_boolean(variable_name[,value])Example:dv.add_boolean('boolean',value=True)ColorColor widget allows you to pick colors easily. When clicked on the
color, a color picker shows up for you to choose a color.add_color(variable_name[,value])initial_value must be a tuple(r, g, b)or a colorcode#xxxxxx.
Example:dv.add_color('color',value=(12,63,85))dv.add_color('color2',value='#0c3f55')...When accessing the color variable,r,g,b,color_codeandtupleparts are available.print(color.r,color.g,color.b,color.color_code,color.tuple)ButtonButton widget allows you to invoke functions manually.add_button(button_name,function_to_invoke)Example:defprint_hello():print('Hello!')dv.add_button('Print Hello',print_hello)Init GUITo initialize the GUI, calldv.init_gui(). You can feed in some
optional arguments in here:
- window_title: Title of the window. Default isVariable Tweaker.font_size: Font size of the labels. Default is16.widget_font_size: Font size of the widgets. Default isfont_size*0.75.default_width: Initial width of the window in pixels (window is resizable). Default is500.theme: Theme of the GUI. Options arelight,darkandnative. Default isdark.color: Color of the theme. Not used when theme isnative. Options areblue,dark-blue,green,sweetkind. Default isdark-bluedv.init_gui(window_title='VT',font_size=20,widget_font_size=16,default_width=1000,theme='dark',color='sweetkind')(light theme with green color)(native theme)Saved VariablesDynamic Variables saves all the variables inside system config file.
After closing and reopening the app, your variables are restored (unless
you specify withvalue=...). This process is unique to every file
(same variable in different files considered different).Init GUI arguments are also saved. If you use light theme once, the next
theme (unless you specify) will be light.Example Applicationimportcv2ascvimportdynamic_variablesasdvdefsave_image():print('image saved')# Set up dynamic variablesdv.add_boolean('colored')dv.add_text('text')dv.add_slider('x',0,100)dv.add_slider('y',0,100)dv.add_color('color')dv.add_dropdown('threshold_type',['None','Normal','Adaptive Gaussian','Adaptive Mean'])dv.add_slider('thresh',0,255)dv.add_slider('block_size',3,201,step=2)dv.add_slider('C',-100,100)dv.add_button('Save Image',save_image)dv.init_gui()# import imageimage=cv.imread('image.png')whilecv.waitKey(20)!=ord('q'):copy=image.copy()# coloredifnotcolored:copy=cv.cvtColor(copy,cv.COLOR_BGR2GRAY)# Apply Thresholdifthreshold_type=='Normal':_,copy=cv.threshold(copy,thresh,255,cv.THRESH_BINARY)elifthreshold_type=='Adaptive Gaussian':copy=cv.adaptiveThreshold(copy,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY,block_size,C)elifthreshold_type=='Adaptive Mean':copy=cv.adaptiveThreshold(copy,255,cv.ADAPTIVE_THRESH_MEAN_C,cv.THRESH_BINARY,block_size,C)ifnotcolored:copy=cv.cvtColor(copy,cv.COLOR_GRAY2BGR)# Put textcopy=cv.putText(copy,text,(x,y),cv.FONT_HERSHEY_SIMPLEX,3,(color.b,color.g,color.r),3)# Show imagecv.imshow('Image',copy)Slider Window