Getting Pick list selected value from other Python file - python

I'm trying to get the value of the dropdown list from another Pyton file in the same project.
This is my code in input.py file:
class Input:
Budget = {'Flexible', 'Variable', 'Fixed'}
def __init__(self):
root =Tk()
root.title('Input window V1')
root.geometry('1300x690')
root.resizable(False, False)
frame = Frame(root, width=1000, height=680)
frame.configure(background="gray28")
frame.pack(fill=BOTH, expand=True)
lbl1 = Label(root, bg="gray28", pady=1, text='Budget:', fg="cyan2" , font=("Helvetica", 20))
lbl1.place(x=240, y=225)
var1 = StringVar(root)
#var1.set("None") # default value (Not in use)
pl1 = OptionMenu(root, var1, *self.Budget )
pl1.config(width=20, bg="GREEN", fg="white")
pl1.place(x=470, y=230)
button1 = Button(root, text="GO Hybrid !", command=dashboard)
button1.config(width=25, bg="white")
button1.place(x=600, y=590)
root.mainloop()
and this is the code in the other file I got and I want to get the selected value of pl1 (getting Budget List selected value).
from input import Input
print(Input.Budget)
print (var1.get())
It does give me all "Budget" list but bot the selected value. this is the Error:
NameError: name 'var1' is not defined
Where I should declare it?
Thanks!

If you’re trying to get var1 from another file, initialize your object first.
a = Input()
print(a.var1)
In your __init__:
self.var1 = StringVar(root)

Related

retrieve selected value from a binded combobox in tkinter

I am trying to retrieve the selected value from a binded combobox.
In the below code, I have binded the combobox named “LSF_Combo” to another combobox “Queue _Combo” . I am trying to retrieve the selected value from the “Queue_Combo”, using Queue_Combo.get() method, but I am not able to do so, since its scope is limited within LSF_fields_Enable(event) inner function. I am able to retrieve the selected value of LSF_Combo using LSF_Combo.get() method.
Please let me know if anyone has suggestions for retrieving the selected value from the “Queue_Combo” combobox and furhter nested comboboxes "MT_Combo: and "Merge_Combo", so that I should be able to access and assign these to a variable outside the "LSF_Fields_Enable" function scope.
For example, If i had selected 'priority' from "Queue_Combo" and had selected "MT" from "MT_Combo", i should be able to assign these strings to variables outisde LSF_Fields_Enable(event) function. From the 'def run_program()' variable 'b' should have sting equals to 'priority' (since i have selected this) and variable 'c' should have string equals to 'MT'.
from tkinter import *
import tkinter as tk
from tkinter import ttk
root = Tk()
root.geometry("800x450")
def Gui_main():
Queue_Combo_list = ['normal', 'priority', 'devices', 'grid']
Queue_Combo = ttk.Combobox(root, values=Queue_Combo_list, width=10, state="readonly")
Merge_Combo_list = ['LST_SET_ML', 'LST_SET_EQU']
Merge_Combo = ttk.Combobox(root, values=Merge_Combo_list, width=12)
MT_Combo_list = ['MT', 'MTFLEX']
MT_Combo = ttk.Combobox(root, values=MT_Combo_list, width=10)
def LSF_Fields_Enable(event):
# global Queue_Combo, VCO_Combo, OS_Combo
VCO_1 = None
if LSF_Combo.get() == 'LSF':
# # section for Queue Type
Queue_Combo_label = tk.Label(root, text="Queue: ").grid(row=2, column=1, padx=5,sticky=W)
Queue_Combo.set("normal")
Queue_Combo.grid(row=2, column=1, padx=55, pady=5, sticky=W)
def MT_MT_flex(choice):
def Manage_run_mode(choice):
# if Merge_Combo.get() == 'LSF_SET_EQU' or 'LSF_SET_MAN':
if Merge_Combo.get() != 'LSF_SET_ML':
label = tk.Label(root, text="CPU No:").grid(row=4, column=3, padx=10,sticky=tk.W)
CPU_radio_button = tk.IntVar()
R1 = Radiobutton(root, text="2", variable=CPU_radio_button, value=1).place(x=50, y=520)
if MT_Combo.get() == 'MT':
# # section for choosing a file version
Merge_Combo_label = tk.Label(root, text="Merge: ").grid(row=6, column=2, padx=10,sticky=W)
Merge_Combo.set("LST_SET_ML")
Merge_Combo.grid(row=6, column=2, padx=65, pady=15, sticky=tk.E)
Merge_Combo.bind('<<ComboboxSelected>>',Manage_run_mode)
MT_Combo_label = tk.Label(root, text="MT/MTflex: ").grid(row=2, column=3, padx=10,sticky=W)
MT_Combo.set("MT")
MT_Combo.grid(row=2, column=4, padx=5, pady=5, sticky=tk.E)
MT_Combo.bind('<<ComboboxSelected>>',MT_MT_flex)
# LSF_Fields_Enable()
LSF_Combo_label = tk.Label(root, text="Fill: ").grid(row=2, column=0, sticky=W)
LSF_Combo_list = ['LSF', 'LOCAL']
LSF_Combo = ttk.Combobox(root, values=LSF_Combo_list, width=10, state="readonly", background='white')
LSF_Combo.set('LSF')
LSF_Combo.grid(row=2, column=0, padx=25, pady=5, sticky=W)
LSF_Combo.bind('<<ComboboxSelected>>', LSF_Fields_Enable)
Queue_Combo.bind('<<ComboboxSelected>>', lambda _: print(Queue_Combo.get()))
MT_Combo.bind('<<ComboboxSelected>>', lambda _: print(MT_Combo.get()))
Merge_Combo.bind('<<ComboboxSelected>>', lambda _: print(Merge_Combo.get()))
# a = LSF_Combo.bind("<Return", LSF_Fields_Enable)
def run_program():
a = LSF_Combo.get()
print(a)
b = Queue_Combo.get()
print(b)
c = MT_Combo.get()
d = Merge_Combo.get()
close = Button(root, text ='close', command= root.destroy).grid(row=10, column=2, pady=60, sticky=E)
check_combo_get =Button (root, text ='check_combo_get', command = run_program).grid(row=10, column=1, padx=15,pady=60, sticky=E)
root.mainloop()
if __name__ == "__main__":
Gui_main()
There is not an easy way to fix this problem in your setup.
But the solution is easy, if you just go ahead and restructure your code a little:
Define the combobox outside the LSF_Fields_Enable function and also asign its values at startup.
Queue_Combo_list = ['normal', 'priority', 'devices', 'grid']
Queue_Combo = ttk.Combobox(root, values=Queue_Combo_list, width=10, state="readonly")
Now bind the ComboboxSelected Event to whatever function you want to have (in this example a simple print).
Queue_Combo.bind('<<ComboboxSelected>>', lambda _: print(Queue_Combo.get()))
And there you go!
Now the print function is executed every time the selection changes for the queue-combobox.
Hope this helps :)
EDIT:
In the updated Code below I reformated your code to make it more readable and to achieve what you wanted to have.
The Value of the ComboBoxes are now assigned to variables ("Queue_Combo_variable", "LSF_Combo_variable").
You can access their values using:
Queue_Combo_variable.get()
I tried my best to comment the entire to be as understandable as possible. Let me now if this is how you imagined it to be!
NEW CODE:
from tkinter import ttk, Tk, Label, Button, StringVar
from tkinter.constants import W, E
def Gui_main():
# Create tkinter (window) instance
root = Tk()
root.geometry("300x250")
# Define LSF_Combox (Combobox)
LSF_Combo_variable = StringVar()
LSF_Combo_label = Label(root, text="Fill: ") # Label
LSF_Combo_label.grid(row=2, column=0, sticky=W)
LSF_Combo_list = ['LSF', 'LOCAL']
LSF_Combo = ttk.Combobox(root, values=LSF_Combo_list, textvariable=LSF_Combo_variable, width=10, state="readonly", background='white')
LSF_Combo.set('LSF')
LSF_Combo.grid(row=2, column=0, padx=25, pady=5, sticky=W) # and place it on the screen
# Define Queue_Combo (Combobox)
Queue_Combo_variable = StringVar()
Queue_Combo_label = Label(root, text="Queue: ") # Label
Queue_Combo_list = ['normal', 'priority', 'devices', 'grid']
Queue_Combo = ttk.Combobox(root, values=Queue_Combo_list, textvariable=Queue_Combo_variable, width=10, state="readonly")
# ==> The Queue_Combo_variable now always stores the value which is currently selected in the queue_combo (box)
#################################################################################################################################
#### ==> Through the following code the specfied function (LSF_Combo_changed) is always called, once the variable value changes #
#################################################################################################################################
LSF_Combo_variable.trace_add("write", lambda _0, _1, _2: LSF_Combo_changed())
Queue_Combo_variable.trace_add("write", lambda _0, _1, _2: print(Queue_Combo_variable.get()))
# If you don't understand, what all of this does, it doesn't matter just now, that you can change the print function to be the function you want to call instead!
# This function is now executed every time when the LSF_Combo (box) is changed (=> "Something has been selected in the left combobox")
def LSF_Combo_changed():
# If "LSF" has been selected, show the Queue_Combo Box
if LSF_Combo.get() == 'LSF':
Queue_Combo.set("normal")
Queue_Combo.grid(row=2, column=1, padx=55, pady=5, sticky=W)
Queue_Combo_label.grid(row=2, column=1, padx=10,sticky=W)
return Queue_Combo.get()
else: # If that hasn't been selected, "forget" the position of the Queue_Combo Box & Label ("Hide them")
Queue_Combo.grid_forget()
Queue_Combo_label.grid_forget()
return None
# Close Button
close_button = Button(root, text ='close', command= root.destroy)
close_button.grid(row=10, pady=60, sticky=E)
# Show window (tkinter instance)
root.mainloop()
if __name__ == "__main__":
Gui_main()

How to enable a disabled Button after filling Entry widgets?

I have 2 Entrys and one button. I want to make that button's state disabled until the two Entrys are filled in. How can I achieve that?
howManyStocksLabel = Label(root, text = "How many stocks do you want to evaluate?")
howManyStocksLabel.grid(row = 1, column = 0)
howManyStocksEntry = Entry(root, borderwidth = 3)
howManyStocksEntry.grid(row = 1, column = 1)
riskLabel = Label(root, text = "Enter risk %")
riskLabel.grid(row = 2, column = 0, sticky = 'w')
riskEntry = Entry(root, borderwidth = 3)
riskEntry.grid(row = 2, column = 1)
nextButton = Button(root, text = "Next!", width = 20, height = 2,state = DISABLED,
fg = 'green', bg = 'white',
command= lambda: myClick(riskEntry, howManyStocksEntry, var))
nextButton.grid(row = 4, column = 1)
I tried to check whether the entries are filled in or not by:
if(riskEntry.get() != ""):
....................
but it just doesn't work.
You need to check if the value is there after the user inputs it. Also, you can use tk.StringVar() as a text variable and trace it.
Here is an example:
import tkinter as tk
def check_entry(*args):
if r1.get() and r2.get():
b1.config(state='normal')
else:
b1.config(state='disabled')
root = tk.Tk()
r1 = tk.StringVar(master=root)
r2 = tk.StringVar(master=root)
e1 = tk.Entry(root, textvariable=r1)
e1.pack()
e2 = tk.Entry(root, textvariable=r2)
e2.pack()
b1 = tk.Button(root, text='Click Me!', state='disabled')
b1.pack()
r1.trace('w', check_entry)
r2.trace('w', check_entry)
root.mainloop()
You will need to use a binding on your entry widgets to check whether the user has entered anything into the entry or not.
This code will fire the check_entry function every time the user types in one of the entry boxes:
riskEntry.bind('<KeyRelease>', check_entry)
howManyStocksEntry.bind('<KeyRelease>', check_entry)
Then your check_entry function might look like this:
def check_entry(event): #event is required for all functions that use a binding
if riskEntry.get() and howManyStocksEntry.get():
nextButton.config(state=NORMAL)
else:
nextButton.config(state=DISABLED)
One way to do it would be to utilize the ability to "validate" their contents that Entry widgets support — see adding validation to an Entry widget — but make it check the contents of multiple Entry widgets and change the state of a Button accordingly.
Below shows how to do this via a helper class that encapsulates most of the messy details needed to make doing it relatively painless. Any number of Entry widgets can be "watched", so it scales well to handle forms consisting of many more than merely two entries.
from functools import partial
import tkinter as tk
from tkinter.constants import *
class ButtonEnabler:
""" Enable/disable a Button depending on whether all specified Entry widgets
are non-empty (i.e. contain at least one character).
"""
def __init__(self, button, *entries):
self.button = button
self.entries = entries
for entry in self.entries:
func = root.register(partial(self.check_entries, entry))
entry.config(validate="key", validatecommand=(func, '%P'))
def check_entries(self, this_entry, new_value):
other_entries = (entry for entry in self.entries if entry is not this_entry)
all_others_filled = all(entry.get() for entry in other_entries)
combined = bool(new_value) and all_others_filled
self.button.config(state=NORMAL if combined else DISABLED)
return True
root = tk.Tk()
howManyStocksLabel = tk.Label(root, text="How many stocks do you want to evaluate?")
howManyStocksLabel.grid(row=1, column=0)
howManyStocksEntry = tk.Entry(root, borderwidth=3)
howManyStocksEntry.grid(row=1, column=1)
riskLabel = tk.Label(root, text="Enter risk %")
riskLabel.grid(row=2, column=0, sticky='w')
riskEntry = tk.Entry(root, borderwidth=3)
riskEntry.grid(row=2, column=1)
nextButton = tk.Button(root, text="Next!", width=20, height=2, state=DISABLED,
fg='green', bg='white', disabledforeground='light grey',
command=lambda: myClick(riskEntry, howManyStocksEntry, var))
nextButton.grid(row=4, column=1)
enabler = ButtonEnabler(nextButton, howManyStocksEntry, riskEntry)
root.mainloop()

Python Tkinter StringVar only displaying Py_Var(number)

I am using Tkinter in python 3.4 to make a text based game, and I cannot figure out how to get a string from an Entry widget, it just returns Py_Var#, # being a number. I have looked at answers to similar questions, but none of them quite line up with what I need and have. Here's the relevant pieces of code:
from tkinter import *
win = Tk()
win.geometry("787x600")
playername = StringVar()
def SubmitName():
playername.get
#messagebox.showinfo("Success", playername)
print(playername)
frame3 = Frame(win)
frame3.pack()
label1 = Label(frame3, text="You awaken in a room, with no memories of yourself or your past. ")
label2 = Label(frame3, text="First, how about you give yourself a name:")
label1.config(font=("Courier", 11))
label2.config(font=("Courier", 11))
entry1 = Entry(frame3, textvariable=playername)
entry1.config(font=("Courier", 11))
label1.grid(row=0, column=0, columnspan=3)
label2.grid(row=1, column=0)
entry1.grid(row=1, column=1)
bnamesub= Button(frame3, text="Submit", command=lambda: SubmitName())
bnamesub.grid()
win.mainloop()
Also, first time using stackoverflow and its reading weird but w/e.
You have two mistakes in SubmitName().
First, you need to get the text like this:
txt = playername.get()
Then you need to print that txt:
print(txt)
By mistake you printed the StringVar variable itself.
from tkinter import *
import pickle
win = Tk()
win.geometry("787x600")
def SubmitName():
playername = entry1.get()
messagebox.showinfo("Success", playername)
print(playername)
frame3 = Frame(win)
frame3.grid()
label1 = Label(frame3, text="You awaken in a room, with no memories of yourself or your past. ")
label2 = Label(frame3, text="First, how about you give yourself a name:")
label1.config(font=("Courier", 11))
label2.config(font=("Courier", 11))
#name entered is a StringVar, returns as Py_Var7, but I need it to return the name typed into entry1.
entry1 = Entry(frame3)
entry1.config(font=("Courier", 11))
label1.grid(row=0, column=0, columnspan=3)
label2.grid(row=1, column=0)
entry1.grid(row=1, column=1)
bnamesub= Button(frame3, text="Submit", command=lambda: SubmitName())
bnamesub.grid()
What I changed:
-deleted playername = StringVar(). We don't really need it;
-changed inside the function: changed playername.get to playername = entry1.get();
-added frame3.grid() (without geometry managment, widgets cannot be shown on the screen.);
-also, a little edit: in Python, comments are created with # sign. So I changed * to #.
I was happy to find a solution here, but all these answers "as it is" are not working with my setting, python3.8, pycharm 2018.2
So if anyone could answer this, it seems that entry1.get() cannot be used as a string. I first wanted to append it in a list, and I did a more simple version to point out the trouble :
from tkinter import *
import pickle
win = Tk()
win.geometry("300x300")
#playername = StringVar()
def SubmitName():
labell = Label(win, text="Little tryup").grid()
playername = entry1.get()
# result about line 11: 'NoneType' object has no attribute 'get'
labelle = Label(win, text=playername).grid()
# print(txt)
label1 = Label(win, text="Enter a name:").grid()
entry1 = Entry(win).grid()
boutonne = Button(win, text="label-it!", command=lambda: SubmitName())
boutonne.grid()
win.mainloop()

Pass values to python script with Tkinter

So I've written a python script that uses the PIL library to format a png passed to it. I want to make the script more user friendly and after looking around I saw the Tkinter library which seemed perfect. Basically the script has five variables that I need to pass to it for it to run. I'm currently using the raw_input() function to asking the user for the following variables:
path_to_png
title
subtitle
source
sample_size
Once received the script runs and exports the formatted png. I've used Tkinter to build those basic inputs as you can see from the picture below but I don't know how to pass the inputed text values and the file path from the choose png button to their respective variables.
from Tkinter import *
from tkFileDialog import askopenfilename
from tkMessageBox import *
app = Tk()
app.title("Picture Formatting")
app.geometry('500x350+200+200')
#
def callback():
chart_path = askopenfilename()
return
def title_data():
title_data = chart_title
return
errmsg = 'Error!'
browse_botton = Button(app, text="Choose png", width=15, command=callback)
browse_botton.pack(side='top', padx=15, pady=15)
# Get chart data
chart_title = StringVar()
title = Entry(app, textvariable = chart_title)
title.pack(padx=15, pady=15)
chart_subtitle = StringVar()
subtitle = Entry(app, textvariable = chart_subtitle)
subtitle.pack(padx=15, pady=15)
chart_source = StringVar()
source = Entry(app, textvariable = chart_source)
source.pack(padx=15, pady=15)
chart_sample_size = IntVar()
sample_size = Entry(app, textvariable = chart_sample_size)
sample_size.pack(padx=15, pady=15)
submit_button = Button(app, text="Submit", width=15)
submit_button.pack(side='bottom', padx=15, pady=15)
app.mainloop()
I have tried your code, and I added some lines:
from Tkinter import *
from tkFileDialog import askopenfilename
from tkMessageBox import *
app = Tk()
app.title("Picture Formatting")
app.geometry('500x350+200+200')
#
def callback():
global chart_path
chart_path = askopenfilename()
return
def title_data():
title_data = chart_title
return
def calculate():
chart_title = title.get()
chart_subtitle = subtitle.get()
chart_source = source.get()
chart_sample_size = sample_size.get()
print "chart_path : ", chart_path
print "chart_title : ", chart_title
print "chart_subtitle : ", chart_subtitle
print "chart_source : ", chart_source
print "chart_sample_size : ", chart_sample_size
#Call your functions here
return
errmsg = 'Error!'
# Get chart data
chart_path = ''
browse_botton = Button(app, text="Choose png", width=15, command=callback)
browse_botton.pack(side='top', padx=15, pady=15)
chart_title = StringVar()
title = Entry(app, textvariable = chart_title)
title.pack(padx=15, pady=15)
chart_subtitle = StringVar()
subtitle = Entry(app, textvariable = chart_subtitle)
subtitle.pack(padx=15, pady=15)
chart_source = StringVar()
source = Entry(app, textvariable = chart_source)
source.pack(padx=15, pady=15)
chart_sample_size = IntVar()
sample_size = Entry(app, textvariable = chart_sample_size)
sample_size.pack(padx=15, pady=15)
submit_button = Button(app, text="Submit", width=15, command=calculate)
submit_button.pack(side='bottom', padx=15, pady=15)
app.mainloop()
I think the problem you want to ask is how to get the text values in the entry widgets and get the path text from the askopenfilename() function. You can use the method Entry.get() to get the text value in certain entry widget.
And you can just use str = askopenfilename() to get the path's text value. But because this line of code is written in a function, you need to declare that it is a global variable or create a class to contain them, or the interpreter will consider that variable is an local variable and it will not be passed to the function calculate() which I added.
Since you didn't use a class to contain the variables, I also use the variables as global variables. It is not a good design. You can consider to create a class instead.
In order to receive the value from an entry widget, you would want to use the get() function. The get() function will return whatever is in the entry widget. For instance in your case: response = sample_size.get() will set the variable response to 0. See this page for more documentation on the entry widget.
Hope this helped.

Tkinter Entry Box 2 is receiving entry box 1 's characters whiles i type in entrybox 1

When I type in entrybox1 it automatically appears in entrybox2. So is like anything that happens entrybox1 happens to entrybox2.
Below is my code
from Tkinter import*
import random
class Love:
def __init__(self):
window = Tk()
window.title("Love Calculator")
window.geometry("300x180")
frame1 = Frame(window)
frame1.pack()
self.lbl = Label(frame1, text = "Love is Pure",fg="white",bg = "blue")
self.lbl2=Label(frame1, text ="are you meant for one another",fg="White",bg = "red")
self.lbl3=Label(frame1,text="Let FIND OUT!!",fg="white",bg = "green")
self.lbl.pack()
self.lbl2.pack()
self.lbl3.pack()
frame2=Frame(window)
frame2.pack()
label = Label(frame2,text = "Your Name")
label2 = Label(frame2, text= "Your Lovers name")
self.msg = StringVar
entry1 = Entry(frame2, textvariable =self.msg)
self.out = StringVar
entry2 = Entry(frame2, textvariable =self.out)
btCalculate=Button(frame2, text="Calculate", command=self.processButton)
label.grid(row=1,column=1)
label2.grid(row=2,column=1)
entry1.grid(row=1,column=2)
entry2.grid(row=2,column=2)
btCalculate.grid(row=4,column=3,sticky=E)
Both of your Entry widgets are effectively using the same textvariable. This is because you are using StringVar wrong. You aren't creating newStringVars, you're merely referencing the class.
In short, you need to do this:
self.msg = StringVar()
... Rather than this:
self.msg = StringVar
Notice the use of ().

Categories