Python Forum
[Tkinter] need help using a returned value to import module
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] need help using a returned value to import module
#11
Yeah, pretty much, I’m still waiting for the pumps to arrive so when I know how long it takes to poor each I’ll adjust the timing
Reply
#12
Did you make any progress?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#13
Some. I only had a couple hours last night and most of that was spent trying to get to grips with how the bit of code you gave me worked. I think I’ve got the jist of it I just need to get it to use the values from the radio buttons to produce the recipe instead of manually entering them
Reply
#14
Here is something I did from your code. Of course you can go OOP and refactor it using classes. Also you may need to adjust the GPIO part (what you said will do anyway)


vending_gui.py

from tkinter import *
import vending

def make_label(title, row):
    lbl = Label(root,text=title, bg="black", fg="#FFEFDB",font="Times 32", width=14)
    lbl.grid(row=row, column=1)
    return lbl

def make_widget(title, items, row):
    lbl = make_label(title, row)
    buttons = []
    row += 1
    for col, drink in enumerate(items):
        radio_button = Radiobutton(root, selectcolor="grey", text=drink, variable=spiritvar, value=drink, 
                               bd=1, bg="#FFEFDB", relief="solid", font="Times 32", width=14)
        radio_button.config(indicatoron=0)
        column =  col%3
        radio_button.grid(row=row, column=column)
        buttons.append(radio_button)
        if column == 2:
            row += 1
    return (row, {'label':lbl, 'buttons':buttons})
    
def drink():
    vending.make_drink(spirit=spiritvar.get(), shots=shotsvar.get(), mixer=mixervar.get())

if __name__ == '__main__':
    root = Tk()
    root.geometry("1024x600+0+0")
    root.configure(bg = "black")
     
    spiritvar = StringVar()
    shotsvar = StringVar()
    mixervar = StringVar()
    widgets = []
    row = 0    
    for option, available_selections in vending.vending_options.items():
        row, widget = make_widget(title=option, items=available_selections, row=row)
        widgets.append(widget)
    b1 = Button(root, text="Make", bd=1, bg="white", relief="solid",
                font="Times 32", width=14, command=drink)
    b1.grid(row=row,column=1)
    root.mainloop()
vending.py
import RPi.GPIO as GPIO
import time
import sys
from collections import OrderedDict

shot_duration = 3
alcoholic_drinks = OrderedDict((('Vodka', 4), ('Gin',17), ('Whiskey',27)))
shots = OrderedDict((('Single', 1), ('Double',2), ('Triple',3)))
soft_drinks = OrderedDict((('Coke',22), ('Lemonade',18), ('Orange',23), ('Soda',24), ('Tonic',25), ('None', 0)))
vending_options = OrderedDict((('Spirits',alcoholic_drinks), ('Shots',shots), ('Mixers',soft_drinks)))

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)    

def make_drink(spirit, num_shots, mixer):
    for bevarage in [alchoholic_drinks, soft_drinks]:
        for pin in bevarage.values(): 
            GPIO.setup(pin, GPIO.OUT) 
            GPIO.output(pin, GPIO.HIGH)

    #prepare drink
    spirit = alcoholic_drinks[spirit]
    num_shots =  shots[num_shots]
    mixer = soft_drinks[mixer]
    GPIO.output(spirit, GPIO.LOW)
    if mixer:
        GPIO.output(mixer, GPIO.LOW)
    for i in range(4):
        time.sleep(shot_duration)
        if i == num_shots:
            GPIO.output(spirit, GPIO.HIGH)
    if mixer:
        GPIO.output(mixer, GPIO.HIGH)
    time.sleep(1)
    GPIO.cleanup()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#15
You absolute legend that will help me no end. I’ve got nothing planned for tonight except writing this code so I’ll let you know how I get on. Thanks
Reply
#16
Note that vending.py is what you now have in 54 files. Also if want go fancy, you can (instead of hardcoding all options - alcoholic_drinks, shots, soft_drinks, vending_options, etc.) you can supply them as json file. In any case now it's easy to extend it as you like with more drinks, options, etc.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#17
Got it working last night. Just need to fix the timings for the drinks but seems to do what I need it to do. Thanks for all your help.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Lightbulb [Tkinter] Tkinter Class Import Module Issue AaronCatolico1 6 2,971 Sep-06-2022, 03:37 PM
Last Post: AaronCatolico1
  How to import entire module ? tonycstech 5 3,254 Oct-21-2020, 06:16 AM
Last Post: tonycstech
  How can import variable beteen classes in same module johnjh 1 1,910 Apr-19-2020, 09:41 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020