I am having a little trouble with this project that I am working on. My project is this GUI application. In my test.py file, I call another file that contains instructions for another GUI window. This is where I am having trouble. In the test.py file, if you click run, a small window will appear. Click TEST in the small window. Then another window will appear that contains text fields if you enter numbers into the text fields for the window and then click enter. My IDE gets these error messages. It says that " ValueError: could not convert string to float: ' ' " My question is how do I fix this so that I do not get this error message? It is supposed to print the input that was entered into the window. I have two files, test.py, and model_objects.py. If you run model_objects.py by itself, it works perfectly. But when I try to import this file into test.py, it does not want to work right. This is programmed in Python. Also, my model_objects.py file is placed in a folder called util in the project. The values that I entered are floating-point values. I am having trouble with this. If you can help, I would greatly appreciate it.
Here is my code:
model_objects.py (This is in a folder called util in the project.)
import tkinter as tk
from tkinter import ttk
from tkinter.ttk import Style
import numpy as np
from util import InputData
class Harmonic_Oscillator:
def __init__(self):
self.type = 0
self.name = "Harmonic Oscillator"
self.nparam = 2
self.label = ["\u03BC", "k"]
self.param = np.zeros(self.nparam, float)
def set_param(self, param_list):
for i in range(self.nparam):
self.param[i] = param_list[i]
return
class Morse_Oscillator:
def __init__(self):
self.type = 1
self.name = "Morse Oscillator"
self.nparam = 3
self.label = ["\u03BC", "De", "a"]
self.param = np.zeros(self.nparam, float)
def set_param(self, param_list):
for i in range(self.nparam):
self.param[i] = param_list[i]
return
class Test_Oscillator:
def __init__(self):
self.type = 2
self.name = "Test Oscillator"
self.nparam = 4
self.mu = 0
self.label = ["a", "b", "c", "d"]
self.param = np.zeros(self.nparam, float)
def set_param(self, param_list):
for i in range(self.nparam):
self.param[i] = param_list[i]
return
def model_prompt(potential_model):
window1 = tk.Tk()
style = Style()
window1.title('PyFGH Parameters')
box_length = 103
for q in range(3):
box_length = box_length + 33 * potential_model[q].nparam
box_len_str = '300x' + str(box_length)
window1.geometry(box_len_str)
entries = []
qvar = np.empty(3, dtype=list)
for i in range(3):
qvar[i] = []
j = 0
y = 5
for q in range(3):
for qparam in range(potential_model[q].nparam):
qvar[q].append(tk.StringVar())
ttk.Label(window1, text=potential_model[q].label[qparam] + " for Q:" + str(q + 1) + ":",
font=("Times New Roman", 15)).place(x=50, y=y)
# set text variable as q1var[j] , each entry will have separate index in the list
a1 = ttk.Entry(window1, textvariable=qvar[q][qparam], font=("Times New Roman", 10)).place(x=140, y=y)
j += 1
y += 35
def enter_button():
for q in range(3):
param_list = []
for qparam in range(potential_model[q].nparam):
param_list.append(qvar[q][qparam].get())
potential_model[q].set_param(param_list) # This is giving me error. Not working properly!!!
for q in range(3):
for qparam in range(potential_model[q].nparam):
print(potential_model[q].param[qparam])
InputData.output.items.model_data = potential_model
print(InputData.output.items.model_data)
window1.destroy()
enter = tk.Button(window1, text='Enter', bd='20', bg='green', fg='white',
command=enter_button).place(x=110, y=y)
window1.mainloop()
def output2():
sections = []
for i in range(3):
if InputData.output.items.v[i] == "Model-Harmonic Oscillator":
sections.append(Harmonic_Oscillator())
elif InputData.output.items.v[i] == "Model-Morse Oscillator":
sections.append(Harmonic_Oscillator())
elif InputData.output.items.v[i] == "Model-Test Oscillator":
sections.append(Harmonic_Oscillator())
#test = [Harmonic_Oscillator(), Morse_Oscillator(), Test_Oscillator()]
#model_prompt(test)
Here is another file called test.py
from util import InputData
from util import model_objects
from util import model_objects
from util.model_objects import Harmonic_Oscillator, Morse_Oscillator, Test_Oscillator, model_prompt
import tkinter as tk
def write_slogan():
test = [Harmonic_Oscillator(), Morse_Oscillator(), Test_Oscillator()]
model_prompt(test)
root = tk.Tk()
frame = tk.Frame(root)
frame.pack()
button = tk.Button(frame,
text="QUIT",
fg="red",
command=quit)
button.pack(side=tk.LEFT)
slogan = tk.Button(frame,
text="TEST",
command=write_slogan)
slogan.pack(side=tk.LEFT)
root.mainloop()
It's a bit esoteric, but the issue is this line in your model_prompt function:
qvar[q].append(tk.StringVar())
The quick fix would be:
qvar[q].append(tk.StringVar(window1))
The tkinter variable types' constructors accept an optional parameter - a handle to the window to which the associated entry widget is attached. If none is supplied, by default, it will pick the first tk.Tk window that was instantiated (in your case, that was root in test.py). The StringVars will update themselves whenever the event queue of the bound window has cleared. Since you spawned a new tk.Tk, this interrupted your root event queue, and since your variables were bound to root, they did not update despite text being entered in the entries.
If you wanted to be super proper, you should look into tk.Toplevel or tkinter.simpledialog to spawn child windows. Your program should never have more than one tk.Tk window in the first place.
i made a little example, i hope it matches yourr mainproblem. if you want to work with classes, in the most cases you need references.
from tkinter import *
class MainWindow(Tk):
def __init__(self):
super(MainWindow, self).__init__()
self.test = Test(self) # test class import
self.outputLbl = Label(self) # output Label
self.outputLbl.pack(side="top", fill="x", ipady=20)
class Test(Frame):
def __init__(self, parent):
super(Test, self).__init__()
self.parent = parent # you can use this way to call between classes
self._input = Entry(self.parent)
self._input.pack()
self._input.bind("<Return>", self.outputMW)
def outputMW(self, event): # function, when pressing return it gives changes the text in your label
var = self._input.get()
self.parent.outputLbl.config(text=var) # self.parent makes the reference to your other class
if __name__ == '__main__':
mw = MainWindow()
mw.geometry("500x500")
mw.mainloop()
Related
I'm trying to make this code close all windows which are opened on windows class. At this moment it only closes few of them. Code works perfectly, but it just dont remove all windows which needs to be removed. Can someone please help me to make this script working?
import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk
import webbrowser
import time
import random
def closeall():
global close
close = True
self = selfstr
parent = parentstr
Window.__init__(self, parent)
class Window(tk.Toplevel):
def __init__(self, parent):
if close == True:
self.destroy()
else:
global selfstr
selfstr = self
global parentstr
parentstr = parent
plusminus_list = ["+", "-"]
plusminus = random.choice(plusminus_list)
plusminus2 = random.choice(plusminus_list)
location = random.randint(1, 1000)
location2 = random.randint(1, 1000)
super().__init__(parent)
self.geometry(f'256x256{plusminus}{location}{plusminus2}{location2}')
self.title('NOESCAPE')
frame = Frame(self, width=256, height=256)
frame.pack()
frame.place(anchor='center', relx=0.5, rely=0.5)
img_list = ["jeff2", "jeff3", "jeff4", "jeff5", "jeff6"]
randomimg = random.choice(img_list)
img = ImageTk.PhotoImage(Image.open(f"{randomimg}.jpg"))
label = Label(frame, image = img)
label.pack()
def on_closing():
plusminus_list = ["+", "-"]
onlywayout()
plusminus = random.choice(plusminus_list)
plusminus2 = random.choice(plusminus_list)
location = random.randint(1, 1000)
location2 = random.randint(1, 1000)
img_list = ["jeff2", "jeff3", "jeff4", "jeff5", "jeff6"]
randomimg = random.choice(img_list)
img = ImageTk.PhotoImage(Image.open(f"{randomimg}.jpg"))
Window(self)
self.protocol("WM_DELETE_WINDOW", on_closing)
self.mainloop()
So you cannot access all Window instances you created for some reason? Then you could save a list with all instances in the Window class as a class attribute. With this list you should be able to destroy all Window instances. Your approach with using the constructor and global variables is a very bad coding style and you shouldn't do anything like this.
Here I created a classmethod so that you don't need the extra function closeall():
import tkinter
class Window(tkinter.Toplevel):
instances = [] # class attribute
def __init__(self, parent):
super().__init__(parent)
self.instances.append(self) # save current instance to the list
# ...
#classmethod
def destroy_all_instances(cls):
for window in cls.instances:
window.destroy()
# Destroy all Window instances:
Window.destroy_all_instances()
I am creating a set of buttons via this function:
from tkinter import *
from random import randint
window = Tk()
window.title("Test")
window.geometry('200x200')
color = ["red","blue","green","yellow","black","purple","orange"]
RandInt = 0
j = 0
h = 0
def ButtonDef(xvar = 0,yvar = 0):
btn = Button(command =lambda:[RandomColor()])
btn.grid()
btn.place(x = xvar*50, y = yvar*50, width = 50, height = 50)
def RandomColor():
RandInt = randint (0,6)
btn.configure(bg = color[RandInt])
while j in range (4):
i = 0
j += 1
while i in range (4):
ButtonDef(i,h)
i += 1
if i == 4:
h += 1
window.mainloop()
However, my RandomColor() function is changing only the color of the very button i have pressed - that is fun too, but i wonder how i can make it randomly change the color of all buttons. When being created by a function, i would have guessed that all buttons that were created are named "btn" since thats the only name i have given them.
How could i address all (or one specific) buttons, out of a group of created-by-function buttons?
Or to put it simply, what name do all of those buttons have? Do they share the name "btn"? Are they assigned a hidden ID?
The reason behind your problem:
The problem is that when this line is executed: btn = Button(command =lambda:[RandomColor()]) by the end of the loop, you get a reference only to the last button which is created. You loose reference to other buttons.
Solution:
You can overcome this problem if you rely on winfo_children().
You have to do 2 steps to fix your issue:
First of all, change: btn = Button(command =lambda:[RandomColor()]) to btn = Button(window, command=lambda:[RandomColor()]). That simply means we attach each created button to a parent widget called window.
Then all that you need to change is RandomColor() function body as follows:
def RandomColor():
for child in window.winfo_children():
child.configure(bg=color[randint(0,6)])
Demo:
This solves your problem but your code is not clean. You can ask for suggestions to improve your code on Code Review website.
EDIT:
Here is a solution for the scenario you described in the comment.
Note that I had to create code from scratch, cleaner one (I know you started only today, so I am not blaming you). In this code, I keep reference for every button I create:
import tkinter as tk
import random
class ChangeBottomRightColor(tk.Frame):
def __init__(self, master):
self.master = master
tk.Frame.__init__(self, self.master)
self.__colors = ["red","blue","green","yellow","black","purple","orange"]
self.configure_gui()
self.create_widgets()
def configure_gui(self):
pass
def create_widgets(self):
self.create_buttons()
def create_buttons(self):
self.buttons = {}
c = 0
for i in range(4):
for j in range(4):
self.buttons['button{}'.format(c)] = tk.Button(self.master)
self.buttons['button{}'.format(c)].grid(row=i, column=j)
self.buttons['button{}'.format(c)].config(width=3, height=3)
self.buttons['button{}'.format(c)].config(command=self.change_bottom_right_button_color)
c += 1
def get_random_color(self):
return random.choice(self.__colors)
def change_bottom_right_button_color(self):
self.buttons['button{}'.format(15)].config(bg=self.get_random_color())
if __name__ == '__main__':
root = tk.Tk()
main_app = ChangeBottomRightColor(root)
root.mainloop()
Demo:
Let try
btn = []
for i in range(16):
btn.append(Button(window))
it will create an array of button. So you can access by btn[i].configure(command=lambda:[RandomColor()]) or something else.
I'm trying to build a tkinter program that will take a search string from a text field on a main window, then create a child window with the results of the search string. Since I'm new tkinter I'm just trying to get the child window created and have the search string passed to that window for now. When running my program I receive the error:
AttributeError: '_tkinter.tkapp' object has no attribute 'Toplevel'
regarding line:
line 23, in search
which is:
results_window = self.parent.Toplevel(self)
Here is the code:
import tkinter as tk
#App Constants
APP_TITLE = 'TKINTER_TEST'
APP_ICON = '\icon\path.ico'
#Geo Constants
MAIN_WIDTH = 600
MAIN_HEIGHT = 300
MAIN_X = '-0'
MAIN_Y = '+0'
class MainApp():
def __init__(self, parent):
self.parent = parent
def get_search(self):
pass
def search(self, search_string):
results_window = self.parent.Toplevel(self)
def build_widgets(self):
search_string = tk.StringVar()
text = tk.Entry(root, textvariable = search_string).pack()
search_cmd = tk.Button(root, text="Search", command=self.search(search_string.get())).pack()
#MAIN
root = tk.Tk()
#root.geometry('500x300-0+0')
root.geometry('{}x{}{}{}'.format(MAIN_WIDTH, MAIN_HEIGHT, MAIN_X, MAIN_Y))
root.title(APP_TITLE)
main = MainApp(root)
main.build_widgets()
root.mainloop()
Am I approaching this the wrong way?
Toplevel is a widget like e.g. Frame
So the line 23 must be :
self.result_window = tk.Toplevel(self.parent)
I am trying to pass a variable from a tkinter combobox to a function called when clicking a 'Run' button. I am relatively new to python and every option I have tried creates an error - mostly that the variable is not defined. I believe this is because I am not defining it in the correct place. Any help is greatly appreciated.
from tkinter import *
from tkinter import ttk
from URL_Generator import crawl_site
listFile = open('regions1.txt','r')
root = Tk()
root.configure()
varItems = StringVar(root, value='')
class MainWindow(Frame):
def __init__(self,master = None):
Frame.__init__(self,master)
self.master = master
self.grid()
self.create_widgets()
def create_widgets(self):
"""Create Window Layout"""
self.label = Label(self, text="List Items").pack()
self.itemCombo = ttk.Combobox(self, width = 16, textvariable = varItems)
self.itemCombo.bind("<Return>", self.itemCombo_onEnter)
self.itemCombo.bind('<<ComboboxSelected>>',self.itemCombo_onEnter)
self.itemCombo['values'] = [l.strip() for l in listFile.readlines()]
self.itemCombo.pack()
self.blank = Label(self,text='').pack()
"""I want to pass the value selected in the combobox to the crawl_region() function when pushing Run"""
self.RunButton = Button(self, text="Run",command = crawl_site.crawl_region(region))
self.RunButton.pack()
def itemCombo_onEnter(self,event):
varItems.set(varItems.get().lower().strip())
mytext = varItems.get().strip()
vals = self.itemCombo.cget('values')
self.itemCombo.select_range(0,END)
print(mytext)
region = mytext
"""I want to pass mytext to the function called when pushing Run"""
if not vals:
self.itemCombo.configure(values = (mytext,))
elif mytext not in vals:
with open('regions1.txt', 'w') as f:
self.itemCombo.configure(values=vals + (mytext,))
f.write("\n".join(vals + (mytext,)))
f.close()
return 'break'
app = MainWindow(root)
root.mainloop()
Sample function called (crawl_site.crawl_region()):
class crawl_site():
def crawl_region(region):
print('passed region '+ str(region))
passed region [] is immediately returned, but nothing happens when I make a selection or press the Run button.
Try the below code.
I've created a class property self.mytext which is set when the combo button is entered itemCombo_onEnter. When the button is pressed the onRunButton function is called. if self.mytext has been set, it will call the crawl_region function with self.mytext as an argument.
from tkinter import *
from tkinter import ttk
from URL_Generator import crawl_site
listFile = open('regions1.txt','r')
root = Tk()
root.configure()
varItems = StringVar(root, value='')
class MainWindow(Frame):
def __init__(self,master = None):
Frame.__init__(self,master)
self.master = master
self.grid()
self.create_widgets()
def create_widgets(self):
"""Create Window Layout"""
self.label = Label(self, text="List Items").pack()
self.itemCombo = ttk.Combobox(self, width = 16, textvariable = varItems)
self.itemCombo.bind("<Return>", self.itemCombo_onEnter)
self.itemCombo.bind('<<ComboboxSelected>>',self.itemCombo_onEnter)
self.itemCombo['values'] = [l.strip() for l in listFile.readlines()]
self.itemCombo.pack()
self.blank = Label(self,text='').pack()
"""I want to pass the value selected in the combobox to the crawl_region() function when pushing Run"""
self.RunButton = Button(self, text="Run",command = self.onRunButton)
self.RunButton.pack()
def onRunButton(self):
if self.mytext:
crawl_site.crawl_region(self.mytext)
def itemCombo_onEnter(self,event):
varItems.set(varItems.get().lower().strip())
mytext = varItems.get().strip()
vals = self.itemCombo.cget('values')
self.itemCombo.select_range(0,END)
print(mytext)
self.mytext = mytext
"""I want to pass mytext to the function called when pushing Run"""
if not vals:
self.itemCombo.configure(values = (mytext,))
elif mytext not in vals:
with open('regions1.txt', 'w') as f:
self.itemCombo.configure(values=vals + (mytext,))
f.write("\n".join(vals + (mytext,)))
f.close()
return 'break'
app = MainWindow(root)
root.mainloop()
Your code wasn't working because of this line
self.RunButton = Button(self, text="Run",command = crawl_site.crawl_region(region))
This immediately calls the method crawl_region with the region as an argument and tries to set the callback of the button to the result of that method.
Another way to 'fix' your problem without creating another function would be to use lambda but I think my method is more readable.
I'm making a very simple program for class that involves multiplying the number of a GUI slider by another number of another GUI slider. But, for some reason when I run the program now, I get an AttributeError saying that 'gui' object has no attribute 'slider1'. Any ideas? Here's the code:
import tkinter
import random
class gui:
def __init__(self):
self.main_window = tkinter.Tk()
#widgets
self.__canvas = tkinter.Canvas(self.main_window,bg='white',width=300,height=10)
self.label = tkinter.Label(self.main_window,text=('Product:',0))
self.slider1 = tkinter.Scale(self.main_window,from_=0, to=12)
self.slider2 = tkinter.Scale(self.main_window,from_=0, to=12)
#packs
self.__canvas.pack()
self.label.pack(side='top')
self.slider1.pack(side='left')
self.slider2.pack(side='right')
self.button = tkinter.Button(self.main_window,text='Click to multiply',command=self.multiply())
self.button.pack(side='bottom')
tkinter.mainloop()
def multiply(self):
x = int(self.slider1.get())
y = int(self.slider2.get())
num = x*y
self.label.config(text=('Product:',num))
gui()
There is a few syntax error in the program, I commented those. As well as you should put orientations on the scales. Here is the code.
import tkinter as tk
class gui:
def __init__(self):
self.root = tk.Tk()
# the widgets
self.button = tk.Button(self.root, text="Multiply!", command=self.multiply)
# you need no '()' for the function when inputing it in tkinter.
self.label = tk.Label(self.root, text="Product: 0") # the '0 must be a string
self.sliderX = tk.Scale(self.root, from_=0, to=12, orient=tk.HORIZONTAL)
self.sliderY = tk.Scale(self.root, from_=0, to=12, orient=tk.VERTICAL)
# add an orient to the scales.
# now pack the widgets.
self.button.pack()
self.label.pack()
self.sliderX.pack()
self.sliderY.pack()
def multiply(self):
x = int(self.sliderX.get())
y = int(self.sliderY.get())
num = str(x * y) # need to turn the int to a string.
self.label.config(text="Product: "+num)
app = gui()
app.root.mainloop()
The reason it isn't working for you is because there is no instance of the program. This is what I do at the very end. Python's garbage collecting collects the instance made with gui() and so Tkinter can't reference an instance of the class.