I'm having issues with my constructor. I get the error __init__() missing 1 required positional argument: 'checkbutton'
Update:
this is my main file:
from GUI_Rootmodule import GUI_Root
gui_root = GUI_Root()
this is my rootmodule file:
from GUI_Unitmodule import Unit
from tkinter import *
class GUI_Root:
def __init__(self):
print("hoi")
window = Tk()
window.title("Project: Embedded Systems")
rootframe = Frame(window, width=1800, height=750)
rootframe.pack()
Unit(rootframe)
window.mainloop()
this is my Unitmodule:
from tkinter import *
class Unit:
def __init__(self, master, checkbutton):
self.frame1 = Frame(master) #Frame voor labels, buttons, entries
self.frame1.pack()
# Checkbutton #
print("test frame")
self.var1 = IntVar()
self.checkbutton = checkbutton(self.frame1, text="Automatisch", variable=self.var1, onvalue= 1, offvalue= 0, pady=20).grid(row=0, column=0, sticky=E)
print("test checkbutton")
# Labels #
self.Extend_Label = Label(self.frame1, text="Uitrol afstand", pady=20).grid(row=2, column=0, sticky=E)
self.Retract_Label = Label(self.frame1, text="Inrol afstand", pady=20).grid(row=3, column=0, sticky=E)
self.Temperture_Label = Label(self.frame1, text="Temperatuur Trigger", pady=20).grid(row=4, column=0, sticky=E)
self.LightIntensity_Label = Label(self.frame1, text="Lichtintensiteit Trigger", pady=20).grid(row=5, column=0, sticky=E)
print("test label")
# Entry #
self.Extend_Entry = Entry(self.frame1).grid(row=2, column=1, sticky=E)
self.Retract_Entry = Entry(self.frame1).grid(row=3, column=1, sticky=E)
self.Temperture_Entry = Entry(self.frame1).grid(row=4, column=1, sticky=E)
self.LightIntensity_Entry = Entry(self.frame1).grid(row=5, column=1, sticky=E)
print("test entry")
# Buttons
self.A = Button(self.frame1, text ="Inrollen", padx=10, pady=20).grid(row=6, column=0)
self.B = Button(self.frame1, text ="Uitrollen", padx=10, pady=20).grid(row=6, column=1)
print("test button")
I've tried to run in it, but it keeps saying that I'm missing an argument which i've put in init(self, master, checkbutton)
Can someone explain what I'm missing here and/or doing wrong ?
Update 2: Without the checkbutton code, the rest works and shows a simple GUI. It's still not clear where exactly i'm missing the argument for checkbutton
You didn't supply all of the required input parameters on this line:
Unit(rootframe)
You supplied master, but are missing checkbutton. The class should be instantiated with all of the required input parameters:
Unit(master, checkbutton)
Alternatively, you can provide default values for the inputs and adjust the code in the function accordingly:
class Unit(master=None,checkbutton=None):
...
However, it looks like maybe you don't intend to pass in a checkbutton variable at all and meant to simply include a tkinter Checkbutton. If that's what you're after, I've included code for that below. I avoided the star import from tkinter import * so the source of the objects is more clear. I also combined things into the same module so the whole thing runs as-is.
import tkinter as tk
def main():
gui_root = GUI_Root()
class GUI_Root:
def __init__(self):
print("hoi")
window = tk.Tk()
window.title("Project: Embedded Systems")
rootframe = tk.Frame(window, width=1800, height=750)
rootframe.pack()
Unit(rootframe)
window.mainloop()
class Unit:
def __init__(self, master):
self.frame1 = tk.Frame(master) #Frame voor labels, buttons, entries
self.frame1.pack()
# Checkbutton #
print("test frame")
self.var1 = tk.IntVar()
self.checkbutton = tk.Checkbutton(self.frame1, text="Automatisch", variable=self.var1, onvalue= 1, offvalue= 0, pady=20).grid(row=0, column=0, sticky=tk.E)
print("test checkbutton")
# Labels #
self.Extend_Label = tk.Label(self.frame1, text="Uitrol afstand", pady=20).grid(row=2, column=0, sticky=tk.E)
self.Retract_Label = tk.Label(self.frame1, text="Inrol afstand", pady=20).grid(row=3, column=0, sticky=tk.E)
self.Temperture_Label = tk.Label(self.frame1, text="Temperatuur Trigger", pady=20).grid(row=4, column=0, sticky=tk.E)
self.LightIntensity_Label = tk.Label(self.frame1, text="Lichtintensiteit Trigger", pady=20).grid(row=5, column=0, sticky=tk.E)
print("test label")
# Entry #
self.Extend_Entry = tk.Entry(self.frame1).grid(row=2, column=1, sticky=tk.E)
self.Retract_Entry = tk.Entry(self.frame1).grid(row=3, column=1, sticky=tk.E)
self.Temperture_Entry = tk.Entry(self.frame1).grid(row=4, column=1, sticky=tk.E)
self.LightIntensity_Entry = tk.Entry(self.frame1).grid(row=5, column=1, sticky=tk.E)
print("test entry")
# Buttons
self.A = tk.Button(self.frame1, text ="Inrollen", padx=10, pady=20).grid(row=6, column=0)
self.B = tk.Button(self.frame1, text ="Uitrollen", padx=10, pady=20).grid(row=6, column=1)
print("test button")
if __name__ == '__main__':
main()
Related
Basically I have a class that inherits from LabelFrame, I want it to show up on the window itself, but I don't see it at all, it doesn't even show up on the window no matter what I do
from tkinter import *
root = Tk()
root['bg'] = 'white'
root.state('zoomed')
class CharacterFrame(LabelFrame):
def __init__(self, master, skin_img, requirement, desc):
super().__init__(master=master, bg='grey29')
self.desc = Label(self, text=desc)
default_char_frame = CharacterFrame(master=root, desc='Default Character.', requirement=0, skin_img='')
default_char_frame.grid(row=0, column=0, sticky='nse')
root.mainloop()
ok, so i was stupid for not implementing the actual code itself, but ill do it now (This isnt actual source code, just the important code, still has same error as actual code):
from tkinter import *
root = Tk()
root['background'] = 'white'
root.state('zoomed')
class SkinFrame(LabelFrame):
def __init__(self, master, skin_img, requirement, desc):
super().__init__(master=master, bg='grey29')
self.desc = Label(self, text=desc)
self.desc.pack()
def show_chars():
button_grid.grid_forget()
skin_lib.grid(row=1, column=0, sticky='nws')
button_skin0 = Button(skin_lib, text="Skin1", relief="flat", width=15, fg="white", bg="darkgrey", command=lambda: show_char_frame(prev, default_char_frame), font=("Arial", 29, "italic"))
button_skin0.grid(row=0, column=0, pady=(0,10))
back = Button(skin_lib, text="Back", width=15, relief="flat", fg="white", bg="maroon", font=("Arial",29,"italic"),
command=lambda: [skin_lib.grid_forget(), button_grid.grid(row=1, column=0, sticky='nws')])
back.grid(row=100, column=0)
def show_char_frame(prev, char_frame):
skin_lib.grid_forget()
default_char_frame.grid(row=1, column=1, sticky='nse')
default_char_frame = SkinFrame(master=root, desc='Default Skin.', requirement=0, skin_img='')
Label(root, text="Game Title", width=68, relief="flat", bg="white", fg="grey", font=("Arial",26,"italic")).grid(row=0, column=0, sticky='n')
button_grid = LabelFrame(root, bg='grey29', relief="groove", pady=10, padx=10)
button_grid.grid(row=1, column=0, sticky='nws')
skin_lib = LabelFrame(root, bg='grey29', relief="groove", pady=10, padx=10)
skins = Button(button_grid, text="Skins", width=15, relief="flat", fg="white", bg="navy", command=show_chars, font=("Arial", 29, "italic"))
skins.grid(row=2, column=0, pady=(0,10))
previous_skinframe
root.mainloop()
you missed to pack() label to add in control
self.desc.pack()
this is complete code
from tkinter import *
root = Tk()
root['bg'] = 'white'
root.state('zoomed')
class CharacterFrame(LabelFrame):
def __init__(self, master, skin_img, requirement, desc):
super().__init__(master=master, bg='grey29')
self.desc = Label(self, text=desc)
self.desc.pack()
default_char_frame = CharacterFrame(master=root, desc='Default Character.', requirement=0, skin_img='')
default_char_frame.grid(row=0, column=0, sticky='nse')
root.mainloop()
in this code what I add self.desc.pack() to show
this is output
I need a bit off help..
In the example below I have two optionsMenus, two entries, and some labels.
What I'm trying to do, is to divide my input from the entry by the labels value, choosen from the optionsMenu, and then show the new value in the next column. But I'm a bit stuck now and can't get it to work.
from tkinter import *
class App(Frame):
def __init__(self, root=None):
Frame.__init__(self, root)
self.materialPrice = {'Brick': 70, 'Rockwool': 50, 'Concrete': 20}
materialvariable1 = StringVar(self, root)
materialvariable1.set("Choose material")
materialvariable2 = StringVar(self, root)
materialvariable2.set("Choose materiale")
self.w1 = OptionMenu(root, materialvariable1, *self.materialPrice, command=self.displayPrice).grid(row=2,
column=0,
columnspan=1,
sticky='WE')
self.w2 = OptionMenu(root, materialvariable2, *self.materialPrice, command=self.displayPrice2).grid(row=3,
column=0,
columnspan=1,
sticky='WE')
self.var = IntVar()
self.var.set(float(0.00))
self.var2 = IntVar()
self.var2.set(float(0.00))
self.entry1 = Entry(root, textvariable=self.var).grid(row=2, column=1)
self.entry2 = Entry(root, textvariable=self.var2).grid(row=3, column=1)
self.priceVarLabel1 = IntVar()
self.priceVarLabel1.set(float(0.00))
self.priceVarLabel2 = IntVar()
self.priceVarLabel2.set(float(0.00))
self.priceVarValue1 = Label(root, textvariable=self.priceVarLabel1, relief='sunken').grid(row=2,
column=2,
columnspan=1,
sticky='WE')
self.priceVarValue2 = Label(root, textvariable=self.priceVarLabel2, relief='sunken').grid(row=3,
column=2,
columnspan=1,
sticky='WE')
self.label1 = Label(root, textvariable=self.displayResult).grid(row=2, column=3)
self.label2 = Label(root, textvariable=self.displayResult2).grid(row=3, column=3)
def displayPrice(self, value):
self.priceVarLabel1.set(self.materialPrice[value])
def displayPrice2(self, value):
self.priceVarLabel2.set(self.materialPrice[value])
def displayResult(self):
self.label1.set(self.entry1 / self.priceVarValue1)
def displayResult2(self):
self.label1.set(self.entry1 / self.priceVarValue1)
root = Tk()
app = App(root)
root.title("help")
root.mainloop()
Just add the division to your function:
def displayPrice(self, value):
self.priceVarLabel1.set(self.materialPrice[value] / self.var.get())
You may want to change the starting value to 1 so that you don't get a ZeroDivisionError right off the bat.
BTW, initializing a widget and laying it out on the same line is a well known bug source. Always use 2 lines.
# very bad:
self.entry1 = Entry(root, textvariable=self.var).grid(row=2, column=1)
# good:
self.entry1 = Entry(root, textvariable=self.var)
self.entry1.grid(row=2, column=1)
I'm new im Python, just started to learn about class and tkinter, so forgive me "messy" code.
I'm trying to enter some string to field nr1, and after click a button, print this string in console and store this value for later:
from tkinter import Tk, BOTH, RIGHT, RAISED, BOTTOM, TOP, X, StringVar
from tkinter.ttk import Frame, Button, Entry
class AD(Frame):
def __init__(self, parent):
Frame.__init__(self, parent, v=None, raw_input=None)
self.parent = parent
self.parent.geometry("250x150+300+300")
self.parent.title("Trolollo")
self.parent.resizable(False, False)
self.inp = None
self.v = StringVar()
self.raw_input = None
self.initUI()
def user_input(self):
global inp
a = self.raw_input(self.v.get())
inp = a
return inp
def initUI(self):
self.pack(fill=BOTH, expand=True)
frame = Frame(self, relief=RAISED, borderwidth=0)
frame.pack(fill=BOTH, expand=True)
self.entry1 = Entry(frame, textvariable=self.v)
self.entry1.pack(side=TOP, fill=X, expand=False, padx=2, pady=2)
self.entry1.focus_set()
rename_button = Button(frame, text="Dispaly text", command = self.user_input())
rename_button.pack(side=TOP, expand=False, padx=2, pady=2)
entry2 = Entry(frame)
entry2.pack(side=TOP, fill=X, expand=False, padx=2, pady=2)
quit_button = Button(self, text="Quit", command=self.quit)
quit_button.pack(side=RIGHT, padx=5, pady=5)
ok_button = Button(self, text="OK")
ok_button.pack(side=RIGHT, padx=5, pady=5)
def main():
root = Tk()
app = AD(root)
root.mainloop()
if __name__ == '__main__':
main()
After executing code, i get:
TypeError: 'NoneType' object is not callable
Any help would me appreciated
ISSUES:
First issue laid in your rename_button's option "command=self.user_input()". You were suppose to name the function
and not execute the function. Putting the () symbol meant you
executed the function when your code loaded, i.e. it executed once
w/o pressing the rename button.
Second issue was the erroneous code in your function user_input. This caused your error msg.
ANSWER: Code with the suggested corrections.
from tkinter import *
from tkinter.ttk import *
class AD(Frame):
def __init__(self, parent):
Frame.__init__(self, parent, v=None, raw_input=None)
self.parent = parent
self.parent.geometry("250x150+300+300")
self.parent.title("Trolollo")
self.parent.resizable(False, False)
self.inp = None
self.v = StringVar()
self.raw_input = None
self.initUI()
def user_input(self):
# Get entry1 value, store it as an attribute and print to console
self.raw_input = self.v.get()
print(self.raw_input)
def initUI(self):
self.frame = Frame(self, relief=RAISED, borderwidth=0)
self.frame.pack(fill=BOTH, expand=True)
self.entry1 = Entry(self.frame, textvariable=self.v)
self.entry1.pack(side=TOP, fill=X, expand=False, padx=2, pady=2)
self.entry1.focus_set()
#self.rename_button = Button(self.frame, text="Dispaly text",
# command = self.user_input())
self.rename_button = Button(self.frame, text="Display text",
command = self.user_input)
self.rename_button.pack(side=TOP, expand=False, padx=2, pady=2)
# You can remove the triple quotes to display these widgets
"""
self.entry2 = Entry(self.frame)
self.entry2.pack(side=TOP, fill=X, expand=False, padx=2, pady=2)
self.quit_button = Button(self.frame, text="Quit", command=self.quit)
self.quit_button.pack(side=RIGHT, padx=5, pady=5)
self.ok_button = Button(self.frame, text="OK")
self.ok_button.pack(side=RIGHT, padx=5, pady=5)
"""
self.pack(fill=BOTH, expand=True)
def main():
root = Tk()
app = AD(root)
root.mainloop()
Your GUI :
SUGGESTIONS:
Do remember to put self. in front of your widgets.
Do test one widget at a time to help you debug your code.
I am using tkinter 8.6 with python 3.5.2 and I trying to create a GUI so that when I click a check box it allows a user to write something into an entry box. However I am getting an error. Below is my code.
from tkinter import *
from tkinter import ttk
def quit():
mainframe.quit()
#Write in Function
def write():
write_in.state(['!disabled']) #Getting Error Here
root = Tk()
root.title("Voting Booth")
#Global variables
var = StringVar()
#Create main widget
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
#Additional widgets
ttk.Label(mainframe, textvariable=var, font=('bold')).grid(column=2, row=0)
vote = ttk.Button(mainframe, text="Vote", command = quit).grid(column=4, row=5, sticky=E)
don_box = ttk.Checkbutton(mainframe, text="Donald Trump").grid(column=2, row=1, sticky=W)
stein_box = ttk.Checkbutton(mainframe, text="Jill Stein").grid(column=2, row=3, sticky=W)
write_box = ttk.Checkbutton(mainframe, text="Write in:", command = write).grid(column=2, row=4, sticky=W)
write_in = ttk.Entry(mainframe, width = 20, state=DISABLED).grid(column=3, row=4, sticky=W)
#Setting variables and widgets
var.set("Presidential Nominees")
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
root.mainloop();
However I am getting the following error when I click the write_in checkbox:
" File "/usr/lib/python3.5/tkinter/__init__.py", line 1553, in __call__
return self.func(*args)
File "./booth_gui.py", line 13, in write
write_in.state(['!disabled'])
AttributeError: 'NoneType' object has no attribute 'state' "
You get a NoneType exception because the write_in object is actually None.
The problem is that you declare write_in as:
write_in = ttk.Entry(...).grid(column=3, row=4, sticky=W)
But the grid method returns a None result.
The call to grid must be separated from the widget declaration:
write_in = ttk.Entry(...)
write_in.grid(column=3, row=4, sticky=W)
About the way you change the widget's state, I'm not really sure it is supposed to work.
Try with the config (or configure) widget method and set the state keyword to NORMAL:
write_in.config(state=NORMAL)
Here is a complete solution:
import tkinter
from tkinter import ttk
class MyApp:
def __init__(self):
self.root = tkinter.Tk()
self.root.title('Voting Booth')
self.labelvar = tkinter.StringVar()
self.votevar = tkinter.StringVar()
self.writevar = tkinter.StringVar()
self.mainframe = ttk.Frame(self.root, padding="3 3 12 12")
self.mainframe.grid(column=0, row=0, sticky='nsew')
self.mainframe.columnconfigure(0, weight=1)
self.mainframe.rowconfigure(0, weight=1)
self.label = ttk.Label(self.mainframe, textvariable=self.labelvar, font=('bold'))
self.vote = ttk.Button(self.mainframe, text='Vote', command=self.quit)
self.don_box = ttk.Radiobutton(self.mainframe, text='Donald Trump', variable=self.votevar, value='trump', command=self.write)
self.stein_box = ttk.Radiobutton(self.mainframe, text='Jill Stein', variable=self.votevar, value='stein', command=self.write)
self.write_box = ttk.Radiobutton(self.mainframe, text='Write in: ', variable=self.votevar, value='write', command=self.write)
self.write_in = ttk.Entry(self.mainframe, textvariable= self.writevar, width=20, state='disabled')
self.label.grid(column=2, row=0)
self.vote.grid(column=4, row=5, sticky='e')
self.don_box.grid(column=2, row=1, sticky='w')
self.stein_box.grid(column=2, row=3, sticky='w')
self.write_box.grid(column=2, row=4, sticky='w')
self.write_in.grid(column=3, row=4, stick='w')
self.labelvar.set('Presidential Nominees')
for child in self.mainframe.winfo_children():
child.grid_configure(padx=5, pady=5)
def quit(self):
self.root.destroy()
def write(self):
if self.votevar.get() == 'write':
self.write_in['state'] = 'normal'
else:
self.write_in['state'] = 'disabled'
self.writevar.set('')
def run(self):
self.root.mainloop()
MyApp().run()
I need to add a button that the user presses to launch a Tkinter.askdirectory, and be able to capture the result. I have tried many examples, but could not find anything that works successfully in my particular case (the case where I am new at this and don't really know what I am doing :)
I have commented out what I have done, so that the original code still works, and so you can sort of see what I have been trying. Which is mostly not working as I am in unfamiliar territory with Tk. Any help greatly appreciated.
#!/usr/bin/env python
import Tkinter
from Tkinter import *
import Tkinter, tkFileDialog
class Values(Tkinter.Tk):
"""docstring for Values"""
def __init__(self, parent):
Tkinter.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
def initialize(self):
self.grid()
stepOne = Tkinter.LabelFrame(self, text=" Create A Playlist ")
stepOne.grid(row=0, columnspan=7, sticky='W',padx=5, pady=5, ipadx=5, ipady=5)
# self.getAdir = tkFileDialog.askdirectory(parent=stepOne, title='Please select a directory')
self.Val1Lbl = Tkinter.Label(stepOne,text="Playlist Name")
self.Val1Lbl.grid(row=0, column=0, sticky='E', padx=10, pady=2)
self.Val1Txt = Tkinter.Entry(stepOne)
self.Val1Txt.grid(row=0, column=1, columnspan=4, pady=2, sticky='WE')
self.Val2Lbl = Tkinter.Label(stepOne,text="Task")
self.Val2Lbl.grid(row=1, column=0, sticky='E', padx=10, pady=2)
self.Val2Var = StringVar()
self.Val2Txt = Tkinter.OptionMenu(stepOne, self.Val2Var, 'Layout','Anim Pass 1','Anim Pass 2', 'Lighting', 'Compositing')
self.Val2Txt.grid(row=1, column=1, columnspan=4, pady=2, sticky='WE')
self.Val3Lbl = Tkinter.Label(stepOne,text="Description")
self.Val3Lbl.grid(row=2, column=0, sticky='E', padx=10, pady=2)
self.Val3Txt = Tkinter.Entry(stepOne)
self.Val3Txt.grid(row=2, column=1, columnspan=4, pady=2, sticky='WE')
# self.Val4Lbl = Tkinter.Label(stepOne,text="Directory")
# self.Val4Lbl.grid(row=3, column=0, sticky='E', padx=10, pady=2)
# self.Val4Var = StringVar()
# self.Val4Var = Tkinter.Button(command=getAdir)
# self.Val4Txt.grid(row=3, column=1, columnspan=4, pady=2, sticky='WE')
self.val1 = None
self.val2 = None
self.val3 = None
# self.val4 = None
SubmitBtn = Tkinter.Button(stepOne, text="Submit",command=self.submit)
SubmitBtn.grid(row=4, column=3, sticky='W', padx=5, pady=2)
def submit(self):
self.val1=self.Val1Txt.get()
if self.val1=="":
Win2=Tkinter.Tk()
Win2.withdraw()
self.val2=self.Val2Var.get()
if self.val2=="":
Win2=Tkinter.Tk()
Win2.withdraw()
self.val3=self.Val3Txt.get()
if self.val3=="":
Win3=Tkinter.Tk()
Win3.withdraw()
# self.val4=self.Val4Var.get()
# if self.val4=="":
# Win4=Tkinter.Tk()
# Win4.withdraw()
self.quit()
if __name__ == '__main__':
app = Values(None)
app.title('Bulk Movie Upload')
app.mainloop() #this will run until it closes
#Print the stuff you want.
print app.val1,app.val2,app.val3 #,app.val4
Here is the revised code, from what I think you meant, but still has the errors in that it is not passing the variable out. I have edited this post, and the code, to simplify.
#!/usr/bin/env python
import Tkinter
from Tkinter import *
import Tkinter, tkFileDialog
class Values(Tkinter.Tk):
"""docstring for Values"""
def __init__(self, parent):
Tkinter.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
def getAdir(self):
self.val5 = tkFileDialog.askdirectory(parent=self, title='Please select a directory')
def initialize(self):
self.grid()
stepOne = Tkinter.LabelFrame(self, text=" Create A Playlist ")
stepOne.grid(row=1, columnspan=8, sticky='W',padx=5, pady=5, ipadx=5, ipady=5)
self.Val5Lbl = Tkinter.Label(stepOne,text="Select Folder Containing Movies")
self.Val5Lbl.grid(row=4, column=0, sticky='E', padx=5, pady=2)
self.Val5Var = StringVar()
self.Val5Txt = Tkinter.Button(stepOne, text="Select Files", command=self.getAdir)
self.Val5Txt.grid(row=4, column=1, columnspan=4, pady=2, sticky='WE')
self.val5 = None
SubmitBtn = Tkinter.Button(stepOne, text="Submit",command=self.submit)
SubmitBtn.grid(row=6, column=3, sticky='W', padx=5, pady=2)
def submit(self):
self.val5=self.Val5Var.get()
if self.val5=="":
Win2=Tkinter.Tk()
Win2.withdraw()
self.quit()
if __name__ == '__main__':
app = Values(None)
app.title('Bulk Movie Upload')
app.mainloop() #this will run until it closes
#Print the stuff you want.
print "Folder is - " + app.val5
One big problem is that you are creating more than one instance of Tk. You can't do that. If you need more windows, create instances if Toplevel.
To call a function to ask for a directory, you need to create a function, and tie that function to a button. What you were doing was actually calling the function before you create the other widgets, rather than wait for the button press.
def getAdir(self):
self.val4 = tkFileDialog.askdirectory(parent=stepOne, title='Please select a directory')
...
self.Val4Var = Tkinter.Button(self, command=self.getAdir)
Note: after I wrote the above, the question was changed to include different code. Here are my comments on that code:
In the revised code, you're not doing what I suggested in my answer. You need to remove this line of code:
self.val5=self.Val5Var.get()
That is because self.val5 already contains the directory returned from askdirectory. It will be a string, not some sort of object with a get method.
You also continue to have the problem that you're opening more than one root window. That will cause additional problems.