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
#1
Im a bit stuck!

Im new to python coding, this is my second project and Ive hit a wall.

Im trying to make a drinks dispenser for a big party

basically Ive made a GUI with 12 radio buttons in 3 sets, (3,3,6) plus an "execute" button.

what i want is to be able to select 3 options from the GUI and when i hit the "execute" button for it to run the appropriate .py file

i have got it working to return a value from the 3 variables like so:

dr1 = StringVar()
dr2 = StringVar()
dr3 = StringVar()


def sel():
    X = dr1.get()
    Y = dr2.get()
    Z = dr3.get()
    return X+Y+Z
The radio buttons can return 54 possible combinations and I have 54 separate scripts written for each one and saved with the same names as the possible returned value. For example- 111,112,113,121,122,123 and so on. (Values and files are words not numbers so I can identify then easily but they’re long so I’m not writing them here).

all possible returned values have a corresponding .py file saved with its own script.

where im getting stuck is using the returned value from sel to run the right file when i hit the "execute" button.

def drink():
    #im stuck here!


b1 = Button(root, text="Make", 
	bd=1,
	bg="white",
	relief="solid",
	font="Times 32",
	width=14,
	command = drink)
When I hit the button b1 I need the main script to run the file generated by "sel"

thanks for any help
Reply
#2
Just use a dictionary "111": "drink_file_for_111", or better yet name the files something like drink.111 so you can generate the file name from the drink selected. Also, one function can do all of this, so get rid of drink() and do it in sel().
Reply
#3
first of all this looks like flawed design. I cannot imagine what these 54 files would do. I would guess it can be reduced to couple of classes/functions... Also, when you say 'run', do you mean really run them, i.e. they are not imported and you use the functionality from within your main/GUI script? It looks like you need
On the good side - it's nice you try to separate logic from the GUI. That would be considered a right approach...
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
#4
The 54 filed represent the different combinations of drinks that can be selected and they control the GPIO pins to despence the right quantities of each ingredient. I probably could make a single script with all the recipes in but I like having a different file for each one.

If I select option 1 from each of the 3 radio buttons sel will generate the value “vodkasinglecoke” if I select opinion 1,1,2 I get a value of “vodkasinglelemonade” and so on, can you give me an example of how to use this value to run the desired script.

If I put import vodkasinglecoke under the def drink() section the button b1 runs the script perfectly so I know both sets of scripts work, I just can’t figure out how to make it run the file based on the returned value from sel.
Reply
#5
(May-30-2018, 08:13 AM)Brave774 Wrote: The 54 filed represent the different combinations of drinks that can be selected and they control the GPIO pins to despence the right quantities of each ingredient. I probably could make a single script with all the recipes in but I like having a different file for each one.


I understand what you said first time, but you are not listening. 54 drinks, each separate file is actually unnecessary huge, repetitive and nightmare-to-maintain code...

Could you provide your code - runnable snippet for your GUI as well several of these files?
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
#6
Here is very basic example that shows what I'm talking about
class Drink:
    def __init__(self, drink_type, sugar, milk):
        self.type = drink_type
        self.sugar = sugar
        self.milk = milk
        
    def __str__(self):
        sugar = {0:'without sugar', 1:'with some sugar', 2:'with a lot of sugar'}
        milk = {True:'with milk', False:'without milk'}
        return ', '.join((self.type, sugar[self.sugar], milk[self.milk]))
        
       
def process_order(order):
    drinks =  {'0':'Coffee', '1':'Tea'}
    drink, sugar, milk = order
    drink = drinks[drink]
    sugar = int(sugar)
    milk = bool(milk)
    return (drink, sugar, milk)

    
def order_drink(order):
    drink, sugar, milk = process_order(order)
    return Drink(drink_type=drink, sugar=sugar, milk=milk)

    
if __name__ == '__main__':
    my_drink = order_drink('011')
    print('I ordered {}'.format(my_drink))
    your_drink = order_drink('120')
    print('You ordered {}'.format(your_drink))
    john_drink = order_drink('000')
    print('John ordered {}'.format(john_drink))
Output:
I ordered Coffee, with some sugar, with milk You ordered Tea, with a lot of sugar, without milk John ordered Coffee, without sugar, without milk >>>
There is a lot that can be improved - e.g. separate VendingMachine class, additional options for drink, etc. but I hope you get the idea
At the moment you can order 2X3X2 = 12 different drinks, without need to maintain 12 files - one for each drink
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
#7
I know my code probably looks like a deranged monkey flinging poo but as i say im new to coding :)

My main GUI code looks like this.

from tkinter import *



   

root = Tk()
root.geometry("1024x600+0+0")
root.configure(bg = "black")

dr1 = StringVar()
dr2 = StringVar()
dr3 = StringVar()



def sel():
	X = dr1.get()
	Y = dr2.get()
	Z = dr3.get()
	return X+Y+Z
        
	

r1 = Radiobutton(root, selectcolor="grey", text="Vodka", variable=dr1, value="vodka", command=sel,
	bd=1,
	bg="#FFEFDB",
	relief="solid",
	font="Times 32",
	width=14)
	
r2 = Radiobutton(root, selectcolor="grey", text="Gin", variable=dr1, value="gin", command=sel,
	bd=1,
	bg="#FFEFDB",
	relief="solid",
	font="Times 32",
	width=14)
	
r3 = Radiobutton(root, selectcolor="grey", text="Whiskey", variable=dr1, value="whiskey", command=sel,
	bd=1,
	bg="#FFEFDB",
	relief="solid",
	font="Times 32",
	width=14)


	
	
r4 = Radiobutton(root, selectcolor="grey", text="Single", variable=dr2, value="single", command=sel,
	bd=1,
	bg="#FFEFDB",
	relief="solid",
	font="Times 32",
	width=14)

r5 = Radiobutton(root, selectcolor="grey", text="Double", variable=dr2, value="double", command=sel,
	bd=1,
	bg="#FFEFDB",
	relief="solid",
	font="Times 32",
	width=14)

r6 = Radiobutton(root, selectcolor="grey", text="Triple", variable=dr2, value="triple", command=sel,
	bd=1,
	bg="#FFEFDB",
	relief="solid",
	font="Times 32",
	width=14)

	
	
	
r7 = Radiobutton(root, selectcolor="grey", text="Coke", variable=dr3, value="coke", command=sel,
	bd=1,
	bg="#FFEFDB",
	relief="solid",
	font="Times 32",
	width=14)
	
r8 = Radiobutton(root, selectcolor="grey", text="Lemonade", variable=dr3, value="lemonade", command=sel,
	bd=1,
	bg="#FFEFDB",
	relief="solid",
	font="Times 32",
	width=14)

r9 = Radiobutton(root, selectcolor="grey", text="Orange Juice", variable=dr3, value="orange", command=sel,
	bd=1,
	bg="#FFEFDB",
	relief="solid",
	font="Times 32",
	width=14)

r10 = Radiobutton(root, selectcolor="grey", text="Tonic Water", variable=dr3, value="tonic", command=sel,
	bd=1,
	bg="#FFEFDB",
	relief="solid",
	font="Times 32",
	width=14)
	
r11 = Radiobutton(root, selectcolor="grey", text="Soda Water", variable=dr3, value="soda", command=sel,
	bd=1,
	bg="#FFEFDB",
	relief="solid",
	font="Times 32",
	width=14)
	
r12 = Radiobutton(root, selectcolor="grey", text="None", variable=dr3, value="none", command=sel,
	bd=1,
	bg="#FFEFDB",
	relief="solid",
	font="Times 32",
	width=14)

r1.config(indicatoron=0)
r2.config(indicatoron=0)
r3.config(indicatoron=0)
r4.config(indicatoron=0)
r5.config(indicatoron=0)
r6.config(indicatoron=0)
r7.config(indicatoron=0)
r8.config(indicatoron=0)
r9.config(indicatoron=0)
r10.config(indicatoron=0)
r11.config(indicatoron=0)
r12.config(indicatoron=0)



r1.grid(row=1,column=0)
r2.grid(row=1,column=1)
r3.grid(row=1,column=2)
r4.grid(row=3,column=0)
r5.grid(row=3,column=1)
r6.grid(row=3,column=2)
r7.grid(row=5,column=0)
r8.grid(row=5,column=1)
r9.grid(row=5,column=2)
r10.grid(row=6,column=0)
r11.grid(row=6,column=1)
r12.grid(row=6,column=2)



label_1 = Label(root,text="Spirit", bg="black", fg="#FFEFDB",font="Times 32", width=14)
label_2 = Label(root,text="Shots", bg="black", fg="#FFEFDB",font="Times 32", width=14)
label_3 = Label(root,text="Mixer", bg="black", fg="#FFEFDB",font="Times 32", width=14)
label_4 = Label(root,bg="black", width=14)
label_5 = Label(root,bg="black", width=14)
label_6 = Label(root,bg="black", width=14)

label_1.grid(row=0,column=1)
label_2.grid(row=2,column=1)
label_3.grid(row=4,column=1)
label_4.grid(row=7,column=1)
#label_5.grid(row=8,column=1)
label_6.grid(row=9,column=1)






def drink():
        
      
        
        
        
	   


b1 = Button(root, text="Make", 
	bd=1,
	bg="white",
	relief="solid",
	font="Times 32",
	width=14,
	command = drink)


	
b1.grid(row=8,column=1)




root.mainloop()
my recipe files look like this

#Vodka, Single, Coke


import RPi.GPIO as GPIO
import time
import sys

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




#   4   = vodka
#   17  = gin
#	27  = whiskey
#	22  = coke
#	18  = lemonade
#	23  = orange
#	24  = soda
#	25  = tonic

pinList = [4,17,27,22,18,23,24,25]


for i in pinList: 
    GPIO.setup(i, GPIO.OUT) 
    GPIO.output(i, GPIO.HIGH)
	

	
GPIO.output(4, GPIO.LOW)
GPIO.output(22, GPIO.LOW)
time.sleep(3)
GPIO.output(4, GPIO.HIGH)
time.sleep(6)
GPIO.output(22, GPIO.HIGH)

time.sleep(1)
GPIO.cleanup()

sys.exit()
# Vodka, Single, Lemonade.


import RPi.GPIO as GPIO
import time
import sys

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




#   4   = vodka
#   17  = gin
#	27  = whiskey
#	22  = coke
#	18  = lemonade
#	23  = orange
#	24  = soda
#	25  = tonic

pinList = [4,17,27,22,18,23,24,25]


for i in pinList: 
    GPIO.setup(i, GPIO.OUT) 
    GPIO.output(i, GPIO.HIGH)
	

	
GPIO.output(4, GPIO.LOW)
GPIO.output(18, GPIO.LOW)
time.sleep(3)
GPIO.output(4, GPIO.HIGH)
time.sleep(6)
GPIO.output(18, GPIO.HIGH)

time.sleep(1)
GPIO.cleanup()

sys.exit()
#Gin, Double, Tonic.


import RPi.GPIO as GPIO
import time
import sys

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




#   4   = vodka
#   17  = gin
#	27  = whiskey
#	22  = coke
#	18  = lemonade
#	23  = orange
#	24  = soda
#	25  = tonic

pinList = [4,17,27,22,18,23,24,25]


for i in pinList: 
    GPIO.setup(i, GPIO.OUT) 
    GPIO.output(i, GPIO.HIGH)
	

	
GPIO.output(17, GPIO.LOW)
GPIO.output(25, GPIO.LOW)
time.sleep(6)
GPIO.output(17, GPIO.HIGH)
time.sleep(3)
GPIO.output(25, GPIO.HIGH)

time.sleep(1)
GPIO.cleanup()

sys.exit()
its a bit long winded but its works(ish) if the coding irritates you enough to clean it up for me i promise i wont to hold it against you Wink
Reply
#8
Please, look at my example - I'm sure you can figure out how to refactor it and get rid of these 54 files. Actually it's even easier because I misunderstood your input to make_drink function
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
#9
Ok thanks for your help I’ll give it a try when I get home from work
Reply
#10
by the way, how do you use shots? my guess is that for 1 shot respective pin is LOW for 3 secs, i.e. 6 sec for double, 9 secs for tripple, soft drinks are always 9 secs. Is that correct?
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


Possibly Related Threads…
Thread Author Replies Views Last Post
Lightbulb [Tkinter] Tkinter Class Import Module Issue AaronCatolico1 6 3,140 Sep-06-2022, 03:37 PM
Last Post: AaronCatolico1
  How to import entire module ? tonycstech 5 3,352 Oct-21-2020, 06:16 AM
Last Post: tonycstech
  How can import variable beteen classes in same module johnjh 1 1,960 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