I'm trying to build youtube downloader application with GUI using python3.
After learning basics of python I'm trying to build one. I'm using "pafy" and "TKinter" modules.
Following are the widgets involved in the GUI
1. One Entry field to input the URL
2. Next to it paste button
3. A media select drop down menu
4. One more drop down menu to list the media quality(depends on previous media input)
5. Finally a download button
Here is my code
from tkinter import *
from tkinter import ttk
import pafy
master = Tk()
media_option = StringVar()
audio_quality_lsit = []
audio_quality_drop_menu = StringVar()
video_quality_lsit = []
video_quality_drop_menu = StringVar()
def url():
global data
url = url_field.get()
data = pafy.new(url)
def audio():
global selected_audio
audio_streams = data.audiostreams
for audio_quality in audio_streams:
audio_quality_lsit.append(audio_quality.bitrate)
selected_audio = audio_quality_drop_menu.get()
print("selected_audio") #Debug Statement
Label(master, text="Audio Bitrate: ").grid(row=7, column=1)
show_drop_menu = OptionMenu(master, audio_quality_drop_menu, *audio_quality_lsit)
show_drop_menu.grid(row=9, column=1)
def video():
global selected_video
video_streams = data.streams
for video_quality in video_streams:
video_quality_lsit.append(video_quality.resolution)
selected_video = video_quality_drop_menu.get()
print("selected_video") #Debug Statement
Label(master, text="Video Quality: ").grid(row=7, column=1)
show_drop_menu = OptionMenu(master, video_quality_drop_menu, *video_quality_lsit)
show_drop_menu.grid(row=9, column=1)
def media_select(self):
global A_V
A_V = media_option.get()
if A_V == "Audio":
audio()
elif A_V == "Video":
video()
def download():
if selected_audio:
print("Audio") #Debug Statement
selected_audio.download(quiet=False)
elif selected_video:
print("Video") #Debug Statement
selected_video.download(quiet=False)
Label(master, text="YouTube URL:").grid(row=0)
url_field = Entry(master)
url_field.grid(row=0, column=1)
Button(master, text='Paste URL', command=url).grid(row=0, column=4, sticky=W, pady=10)
Button(master, text='Download', command=download).grid(row=12, column=4, sticky=W, pady=10)
Label(master, text="Media Type: ").grid(row=3)
media_drop_menu = OptionMenu(master, media_option, "Audio", "Video", command=media_select)
media_drop_menu.grid(row=5, column=0)
mainloop()
I'm stuck at "download function". Am I using the function properly?
Is there any problem in calling the function?
And one more thing, Sorry if it is dump question.
Why we have to do this,
from tkinter import *
from tkinter import ttk
According to my understanding when we use "*", will import all the modules present in the library. Again why we have to use "from tkinter import ttk".
Please help me.
UPDATE:
#JoshuaNixon Thanks for the replay.
I didn't know much about "classes" So first I learnt basics of it and I made some changes.
Here is the code:
from tkinter import *
import pafy
class YouTubeDownloader:
def __init__(self,master):
self.master = master
master.title("Youtube Downloader")
self.media_option = StringVar()
self.audio_quality_lsit = []
self.audio_quality_drop_menu = StringVar()
self.video_quality_lsit = []
self.video_quality_drop_menu = StringVar()
Label(master, text="YouTube URL:").grid(row=0)
self.url_field = Entry(master)
self.url_field.grid(row=0, column=1)
Button(master, text='Paste URL', command=self.url).grid(row=0, column=4, sticky=W, pady=10)
Button(master, text='Download', command=self.download).grid(row=12, column=4, sticky=W, pady=10)
Label(master, text="Media Type: ").grid(row=3)
media_drop_menu = OptionMenu(master, self.media_option, "Audio", "Video", command=self.media_select)
media_drop_menu.grid(row=5, column=0)
def url(self):
global data
url = self.url_field.get()
data = pafy.new(url)
def media_select(self):
global A_V
A_V = self.media_option.get()
if A_V == "Audio":
audio()
elif A_V == "Video":
video()
def audio():
global selected_audio
audio_streams = self.data.audiostreams
for audio_quality in audio_streams:
audio_quality_lsit.append(audio_quality.bitrate)
selected_audio = audio_quality_drop_menu.get()
print("selected_audio") #Debug Statement
Label(master, text="Audio Bitrate: ").grid(row=7, column=1)
show_drop_menu = OptionMenu(master, audio_quality_drop_menu, *audio_quality_lsit)
show_drop_menu.grid(row=9, column=1)
def video(self):
global selected_video
video_streams = self.data.streams
for video_quality in video_streams:
video_quality_lsit.append(video_quality.resolution)
selected_video = video_quality_drop_menu.get()
print("selected_video") #Debug Statement
Label(master, text="Video Quality: ").grid(row=7, column=1)
show_drop_menu = OptionMenu(master, video_quality_drop_menu, *video_quality_lsit)
show_drop_menu.grid(row=9, column=1)
def download(self):
if selected_audio:
print("Audio") #Debug Statement
self.selected_audio.download(quiet=False)
elif selected_video:
print("Video") #Debug Statement
self.selected_video.download(quiet=False)
root = Tk()
my_youtube = YouTubeDownloader(root)
root.mainloop()
When I run the code, I get the GUI. After copy pasting URL field, I get error at "media_select()" saying that
"TypeError: media_select() takes 1 positional argument but 2 were given".
You suggested to make use of two classes. As I'm still beginner in this, I got little confused in the "self" argument. So I used single class.
Can you please tell me where I'm going wrong.
Thank You.
All the syntax errors have been solved. Only one exception is left that I think you can solve.
When you use a class use self.* for referring,
from Tkinter import *
import pafy
class YouTubeDownloader:
def __init__(self,master):
self.master = master
master.title("Youtube Downloader")
self.media_option = StringVar()
self.audio_quality_lsit = []
self.audio_quality_drop_menu = StringVar()
self.video_quality_lsit = []
self.video_quality_drop_menu = StringVar()
Label(master, text="YouTube URL:").grid(row=0)
self.url_field = Entry(master)
self.url_field.grid(row=0, column=1)
Button(master, text='Paste URL', command=self.url).grid(row=0, column=4, sticky=W, pady=10)
Button(master, text='Download', command=self.download).grid(row=12, column=4, sticky=W, pady=10)
Label(master, text="Media Type: ").grid(row=3)
media_drop_menu = OptionMenu(master, self.media_option, "Audio", "Video", command=self.media_select)
media_drop_menu.grid(row=5, column=0)
self.data = []
def url(self):
global data
url = self.url_field.get()
self.data = pafy.new(url)
def media_select(self, a):
global A_V
A_V = self.media_option.get()
if A_V == "Audio":
# for functions of same class use self.function()
self.audio()
elif A_V == "Video":
self.video()
def audio(self):
global selected_audio
audio_streams = self.data.audiostreams
for audio_quality in audio_streams:
self.audio_quality_lsit.append(audio_quality.bitrate)
selected_audio = self.audio_quality_drop_menu.get()
print("selected_audio") #Debug Statement
Label(self.master, text="Audio Bitrate: ").grid(row=7, column=1)
show_drop_menu = OptionMenu(self.master, self.audio_quality_drop_menu, *self.audio_quality_lsit)
show_drop_menu.grid(row=9, column=1)
def video(self):
global selected_video
video_streams = self.data.streams
for video_quality in video_streams:
self.video_quality_lsit.append(video_quality.resolution)
selected_video = self.video_quality_drop_menu.get()
print("selected_video") #Debug Statement
Label(self.master, text="Video Quality: ").grid(row=7, column=1)
show_drop_menu = OptionMenu(self.master, self.video_quality_drop_menu, *self.video_quality_lsit)
show_drop_menu.grid(row=9, column=1)
def download(self):
if selected_audio:
print("Audio") #Debug Statement
self.selected_audio.download(quiet=False)
elif selected_video:
print("Video") #Debug Statement
self.selected_video.download(quiet=False)
root = Tk()
my_youtube = YouTubeDownloader(root)
root.mainloop()
Related
I wrote code that checks availability of sites.
And to change the status of sites after a user defined time put after() before the output function, but for some reason the status is updated before the specified time, what's wrong?
My goal is to enter time for each site (it can be different) and only after this time, the status is updated
Code:
import tkinter as tk
from tkinter import ttk
import requests
import time
from tkinter import *
from tkinter import messagebox
data_list = []
window = Tk()
window.geometry('400x700')
window.title("SiteChecker")
def set_input(obj, value):
obj.delete(1.0, "END")
obj.insert("END", value)
def SiteCheck():
for data in data_list:
url = data[0].get()
status = data[2]
if not str(url).startswith('http'):
continue
print(url)
Get_Response = None
try:
Get_Response = requests.get(url)
except:
status.config(text='status bad')
continue
if Get_Response.status_code == 200:
status.config(text='status ok')
window.after(int(data[1].get()) * 1000, SiteCheck)
pass
else:
status.config(text='status bad')
window.after(int(data[1].get()) * 1000, SiteCheck)
def clicked():
txt = Entry(window, width=18)
txt.grid(column=0, pady=8)
txt_row = txt.grid_info()['row']
tim = Entry(window, width=3)
tim.grid(row=txt_row, column=1, pady=8)
txt_row = tim.grid_info()['row']
result1 = Label(window, text="status")
result1.grid(row=txt_row, column=2, pady=8)
data_list.append([txt, tim, result1])
lbl1 = Label(window, text="Enter references:")
lbl1.grid(column=0, row=1)
lbl2 = Label(window, text="Enter the test time: ")
lbl2.grid(column=1, row=1)
lbl3 = Label(window, text="Availability status ")
lbl3.grid(column=2, row=1)
for loop in range(2, 6):
txt1 = Entry(window, width=18)
txt1.grid(column=0, row=loop, pady=8)
tim1 = Entry(window, width=3)
tim1.grid(column=1, row=loop, pady=8)
result1 = Label(window, text="status")
result1.grid(column=2, row=loop, pady=8)
data_list.append([txt1, tim1, result1])
btn = Button(window, text="Add another site", command=clicked)
btn.grid(column=1, row=0)
Check_Button = Button(
window,
command=SiteCheck,
text='Start checking',
)
Check_Button.grid(row=0, column=2)
window.mainloop()
One way you could do this is to create a dictionary as a global variable that stores details for each site listed site_ref, then you could split up the SiteCheck function into two separate functions, one that is called when the user presses the the Check_Button button and another that controls the timed intervals and updating the status message.
For example:
import tkinter as tk
from tkinter import ttk
import requests
import time
from tkinter import *
from tkinter import messagebox
data_list = []
site_ref = {} # stores details for each site
window = Tk()
window.geometry('400x700')
window.title("SiteChecker")
def set_input(obj, value):
obj.delete(1.0, "END")
obj.insert("END", value)
def check_status(url):
"""this is the callback function that sends the request and
updates the status then it reschedules itself
"""
time = site_ref[url]["time"] # get details from `site_ref`
status = site_ref[url]["status"]
try:
Get_Response = requests.get(url)
except:
status.config(text="status bad")
return
if Get_Response.status_code == 200:
status.config(text="status ok")
else:
status.config(text='status bad')
# reschedule call to callback
window.after(time*1000, site_ref[url]["func"])
def SiteCheck():
"""This function is triggered by button and collects information
about each site, and schedules the first status check only.
"""
for data in data_list:
url = data[0].get()
if not str(url).startswith('http'):
continue
time = int(data[1].get())
status = data[2]
site_ref.setdefault(url, # use the url as the key for dictionary
{"time": time, # store the interval
"func": lambda x=url: check_status(x), # store callback function
"status": status}) # store the status widget
# then schedule the first call to the callback function
window.after(time * 1000, site_ref[url]["func"])
def clicked():
txt = Entry(window, width=18)
txt.grid(column=0, pady=8)
txt_row = txt.grid_info()['row']
tim = Entry(window, width=3)
tim.grid(row=txt_row, column=1, pady=8)
txt_row = tim.grid_info()['row']
result1 = Label(window, text="status")
result1.grid(row=txt_row, column=2, pady=8)
data_list.append([txt, tim, result1])
lbl1 = Label(window, text="Enter references:")
lbl1.grid(column=0, row=1)
lbl2 = Label(window, text="Enter the test time: ")
lbl2.grid(column=1, row=1)
lbl3 = Label(window, text="Availability status ")
lbl3.grid(column=2, row=1)
for loop in range(2, 6):
txt1 = Entry(window, width=18)
txt1.grid(column=0, row=loop, pady=8)
tim1 = Entry(window, width=3)
tim1.grid(column=1, row=loop, pady=8)
result1 = Label(window, text="status")
result1.grid(column=2, row=loop, pady=8)
data_list.append([txt1, tim1, result1])
btn = Button(window, text="Add another site", command=clicked)
btn.grid(column=1, row=0)
Check_Button = Button(
window,
command=SiteCheck,
text='Start checking',
)
Check_Button.grid(row=0, column=2)
window.mainloop()
class Menu():
def __init__(self):
pass
def menuWindow(self):
menu = Tk()
menu.title("Menu")
menu.geometry("300x250")
menuFrame = Frame(menu)
menuFrame.pack()
menu = Menu()
menuLabel = Label(menuFrame, font="Helvetica 18 bold", text="MENU")
menuLabel.grid(row=0, column=0, sticky=W)
cDirF = TaskWindowMaker()
cDir = Button(menuFrame, text="Create Directory", command=cDirF.cDirWindow, height=2,width=20)
cDir.grid(row=2, column=0, sticky=W)
cProfF = TaskWindowMaker()
cProf = Button(menuFrame, text="Create Profiles", command=cProfF.cProfWindow, height=2,width=20)
cProf.grid(row=3, column=0, sticky=W)
dProfF = TaskWindowMaker()
dProf = Button(menuFrame, text="Delete Profiles", command=dProfF.dProfWindow, height=2,width=20)
dProf.grid(row=6, column=0, sticky=W)
mSenderF = TaskWindowMaker()
mSender = Button(menuFrame, text="Make Sender", command=mSenderF.mSenderWindow, height=2,width=20)
mSender.grid(row=8, column=0, sticky=W)
sendParcelF = TaskWindowMaker()
sParcel = Button(menuFrame, text="Send Parcel", command=sendParcelF.sendParcelWindow, height=2,width=20)
sParcel.grid(row=10, column=0, sticky=W)
class ProfileDeveloper(object):
def __init__(self,):
pass
def create_folder_profiles(self):
global directoryNamecProfWindow
y = False
while y == False:
path = os.getcwd()
print(path)
y = numbercProfWindow.get()#int(input("Number of profiles: "))
print(y)
#y = int(y)
p = os.getcwd()+("\profiles")#Reading number file in profiles
n = ("number")
d = os.path.join(p,n)
try:
dirName = directoryNamecProfWindow.get()#input("Directory name :")
print(dirName+"OO")
path = os.getcwd()+("\profiles")
folderDir = os.path.join(path, dirName)
with open(folderDir+"profile.txt","x") as f:
print("Opened.")
except FileNotFoundError:
print("File not found.")
except FileExistsError:
messagebox.showerror("Error","File already exists.")
for a in range(y):
with open(d+".txt","r") as z:
num = z.read()
num = int(num)
newNum = (int(num)+1)
numProf = ("profile"+str(newNum))
with open(d+".txt","w") as file:
file.write(str(newNum))
path = os.getcwd()+("\profiles")
folderDir = os.path.join(path, dirName,numProf)
with open(folderDir+".txt","x") as f:
print("Created '"+numProf+"'.")
y = True
#except:
print("Saved to "+folderDir+"\n")
class TaskWindowMaker(object):
def __init__(self):
pass
def cProfWindow(self):
global numbercProfWindow
cProf = Tk()
cProf.title("Create Profiles")
cProf.geometry("300x250")
cProfFrame = Frame(cProf)
cProfFrame.pack()
titleLabel = Label(cProfFrame, font="Helvetica 18 bold", text="Create Profiles")
titleLabel.grid(row=0, column=0, sticky=W)
menuLabel = Label(cProfFrame, text="Folder Name")
menuLabel.grid(row=1, column=0, sticky=W)
directoryNamecProfWindow = StringVar()
cDirEntry = Entry(cProfFrame, textvariable=directoryNamecProfWindow)
cDirEntry.grid(row=1, column=1, sticky=W)
menuLabel = Label(cProfFrame, text="Number of Profiles")
menuLabel.grid(row=2, column=0, sticky=W)
numbercProfWindow = StringVar()
cDirEntry = Entry(cProfFrame, textvariable=numbercProfWindow)
cDirEntry.grid(row=2, column=1, sticky=W)
cProfF = ProfileDeveloper(directoryNamecProfWindow)
cDir = Button(cProfFrame, text="Enter", command=cProfF.create_folder_profiles, height=2,width=20)
cDir.grid(row=3, column=0, sticky=W)
start = Menu()
start.menuWindow()
This is my code. I'm opening multiple Windows. Then when I enter in an input box in
numbercProfWindow = StringVar()
cDirEntry = Entry(cProfFrame, textvariable=numbercProfWindow)
cDirEntry.grid(row=2, column=1, sticky=W)
I'm then using the button:
cProfF = ProfileDeveloper(directoryNamecProfWindow)
cProf = Button(cProfFrame, text="Enter", command=cProfF.create_folder_profiles, height=2,width=20)
cProf.grid(row=3, column=0, sticky=W)
To send the input to ProfileDeveloper.create_folder_profiles
When it's sent to ProfileDeveloper.create_folder_profiles the program doesn't recognise the input, as in it's not '.get()ing it'.
I'm quite new so don't know how to pass variables between different functions and using the .get() function or global variables.
The main aim of the program is creating a profiles (txt files) in a specified folder. Instead of it being a script in shell I'm trying to add a Tkinter GUI.
Hopefully this explains it as I've asked before and people just get too confused. There's still a lot of code which I don't show but the confusion between passing variables continues throughout the code so not sure. The code was really long and just running in Shell then I added the Tkinter GUI and it's complicated it all by doubling variables and now this, so any help is greatly appreciated.
Thanks :)
Does this example help You understand better what You can do:
from tkinter import Tk, Entry, Button
root = Tk()
class UserInput:
def __init__(self, master):
self.master = master
self.entry = Entry(self.master)
self.entry.pack()
self.button = Button(self.master, text='Submit', command=self.submit)
self.button.pack()
def submit(self):
entry_var = self.entry.get()
print(entry_var)
user_input = UserInput(root)
root.mainloop()
I am trying to get the input of what page number the user wants. They should type in a number, and click the submit button. To test it, I just want to print whatever they typed and then close the window. I've been following: http://effbot.org/tkinterbook/entry.htm as a guide, but I am stumped.
Why does
print(temp)
not print out a number to console?
right now it prints out:
<bound method IntVar.get of <tkinter.IntVar object at 0x000001FBC85353C8>>
I've cleaned up the code a little bit:
import sys
from file import *
from page import *
from view import View
import tkinter as tk
from tkinter import *
class ViewGui:
def __init__(self):
#Included in the class, but unrelated to the question:
self._file = File("yankee.txt", 25)
self.pages = self._file.paginate()
self.initial_list = self.pages[0].readpage(self._file.fo)
self.initial_string = ''.join(self.initial_list)
# Create root
self.root = Tk()
self.root.wm_title("yankee.txt - page 1")
# Create frame for buttons
self.bframe = Frame(self.root)
self.bframe.pack(side=BOTTOM, fill=X)
self.tbutton = tk.Button(self.bframe, text="Top", command=lambda a="top": self.clicks(a)).pack(side=LEFT, expand=1, fill=X)
self.bbutton = tk.Button(self.bframe, text="Bottom", command=lambda a="bottom": self.clicks(a)).pack(side=LEFT, expand=1, fill=X)
self.ubutton = tk.Button(self.bframe, text="Up", command=lambda a="up": self.clicks(a)).pack(side=LEFT, expand=1, fill=X)
self.dbutton = tk.Button(self.bframe, text="Down", command=lambda a="down": self.clicks(a)).pack(side=LEFT, expand=1, fill=X)
self.pbutton = tk.Button(self.bframe, text="Page", command=lambda a="page": self.pageclicks()).pack(side=LEFT, expand=1, fill=X)
self.qbutton = tk.Button(self.bframe, text="Quit", command=quit).pack(side=LEFT, expand=1, fill=X)
# Create and pack Text
self.T = Text(self.root, height=35, width=60, wrap=NONE)
self.T.pack(side=TOP, fill=X)
# Create and pack Scrollbar
self.S = Scrollbar(self.root, orient=HORIZONTAL, command=self.T.xview)
self.S.pack(side=BOTTOM, fill=X)
# Attach Text to Scrollbar
self.T.insert('1.0', self.initial_string)
self.T.config(xscrollcommand=self.S.set, state=DISABLED)
self.S.config(command=self.T.xview)
def pageclicks(self):
print("pageClicks is getting called at least...")
pop = Tk()
pop.wm_title("Page Number")
pop.label = Label(pop, text="Enter a Page Number:", width=35)
pop.label.pack(side=TOP)
pop.entrytext = IntVar()
Entry(pop, textvariable=pop.entrytext).pack()
pop.submitbuttontext = StringVar()
Button(pop, text="Submit", command=lambda a=pop: self.submitted(a)).pack(side=LEFT, pady=5, padx=40)
pop.cancelbuttontext = StringVar()
Button(pop, text="Cancel", command=pop.destroy).pack(side=LEFT, pady=5, padx=40)
def submitted(self, a):
print('submitted is getting called')
temp = (a.entrytext.get)
print(temp)
def clicks(self, a):
print("you clicked clicks with the " + a)
self.root.wm_title(self._file.filename + " - Page " + self._file.buttonHandler(a))
if __name__ == "__main__":
vg = ViewGui()
vg.root.mainloop()
When you create a new window you should not use Tk(), you must use tk.Toplevel()
Must change:
pop = Tk()
to
pop = tk.Toplevel()
You should also use get(), not just get. Must change:
temp = (a.entrytext.get)
to
temp = a.entrytext.get()
Code:
def pageclicks(self):
print("pageClicks is getting called at least...")
pop = tk.Toplevel()
pop.wm_title("Page Number")
pop.label = Label(pop, text="Enter a Page Number:", width=35)
pop.label.pack(side=TOP)
pop.entrytext = IntVar()
Entry(pop, textvariable=pop.entrytext).pack()
pop.submitbuttontext = StringVar()
Button(pop, text="Submit", command=lambda a=pop: self.submitted(a)).pack(side=LEFT, pady=5, padx=40)
pop.cancelbuttontext = StringVar()
Button(pop, text="Cancel", command=pop.destroy).pack(side=LEFT, pady=5, padx=40)
def submitted(self, a):
print('submitted is getting called')
temp = a.entrytext.get()
print(temp)
Made changes to these two methods and now it is working.
def pageclicks(self):
print("pageClicks is getting called at least...")
pop = Tk()
pop.wm_title("Page Number")
pop.label = Label(pop, text="Enter a Page Number:", width=35)
pop.label.pack(side=TOP)
pop._entry = Entry(pop)
pop._entry.pack()
pop._entry.focus_set()
Button(pop, text="Submit", command=lambda a=pop: self.submitted(a)).pack(side=LEFT, pady=5, padx=40)
Button(pop, text="Cancel", command=pop.destroy).pack(side=LEFT, pady=5, padx=40)
def submitted(self, _pop):
temp = _pop._entry.get()
print(temp)
_pop.destroy()
I would like to know how to create a dynamic button that calls the function selected in a OptionMenu Widget.
In the penultimate line [-2], I substituted "command=daily_return" by "command=var" but it does not work.
Any suggestions?
Best
Working code
from Tkinter import *
import Tkinter
import tkMessageBox
master = Tk()
myvar_1 = IntVar()
myvar_2 = IntVar()
myvar_3 = StringVar()
myvar_4 = IntVar()
myvar_5 = IntVar()
myvar_6 = IntVar()
myvar_7 = IntVar()
#
def daily_return(*args):
print "The start date is ", var.get(), "+", myvar_1.get(),"-", myvar_4.get(), "-", myvar_6.get(), "and the end date is", myvar_2.get(),"-", myvar_5.get(), "-", myvar_7.get(), " for the stock ticker:", myvar_3.get(), "."
def cumulative_return(*args):
print "The start date is ", myvar_1.get(), "the cumulative return."
def value_at_risk(*args):
print "The start date is ", myvar_1.get(), "the value at risk."
Label(master, text="Start Date (DD-MM-YYYY)").grid(row=0)
Label(master, text="End Date (DD-MM-YYYY)").grid(row=1)
Label(master, text="Stock Ticker").grid(row=2)
##
text_entry_1 = Entry(master, textvariable=myvar_1)
text_entry_1.pack()
text_entry_2 = Entry(master, textvariable=myvar_2)
text_entry_2.pack()
text_entry_3 = Entry(master, textvariable=myvar_3)
text_entry_3.pack()
text_entry_4 = Entry(master, textvariable=myvar_4)
text_entry_4.pack()
text_entry_5 = Entry(master, textvariable=myvar_5)
text_entry_5.pack()
text_entry_6 = Entry(master, textvariable=myvar_6)
text_entry_6.pack()
text_entry_7 = Entry(master, textvariable=myvar_7)
text_entry_7.pack()
#
var = StringVar()
var.set('Choose function')
choices = ['cumulative_return', 'daily_return', 'value_at_risk']
option = OptionMenu(master, var, *choices)
option.pack()
##
text_entry_1.grid(row=0, column=1)
text_entry_2.grid(row=1, column=1)
text_entry_3.grid(row=2, column=1)
text_entry_4.grid(row=0, column=2)
text_entry_5.grid(row=1, column=2)
text_entry_6.grid(row=0, column=3)
text_entry_7.grid(row=1, column=3)
option.grid(row=4, column=0)
sf = "Quant Program"
#
def quit():
global root
master.destroy()
#
master.title("Quant Program")
Button(master, text='Quit', command=quit).grid(row=4, column=4, sticky=W, pady=4)
Button(master, text='Show', command=daily_return).grid(row=4, column=1, sticky=W, pady=4)
mainloop( )
Sometimes the simplest solution is Good Enough:
def do_something():
# define a mapping from the choice value to a function name
func_map = {
"daily_choices": daily_choices,
"value_at_risk": value_at_risk,
"cumulative_return": cumulative_return,
}
# using the map, get the function
function = func_map[var.get()]
# call the function
function()
...
Button(..., command=do_something)
So in my program, whenever I put the grid_remove() function it usually deletes the widget. Whenever I run the program for the first time (that is, not losing any points by guessing wrongly), it deletes most of the widgets. However, whenever I guess wrong and get points taken away (which works fine), the widgets lazily remain on the window. Any help?
Here's the code: (I think that the error occurs somewhere after def numright and def wordguess)
import random
from tkinter import *
from tkinter import messagebox
text_doc = open("test.txt", "r")
text = text_doc.read()
numbOfletters = len(text)
random_letter = random.randint(0,numbOfletters-1)
letter = text[random_letter]
points = 50
username = ""
is_there = 0
class GUIFramework(Frame):
def __init__(self,master=None):
Frame.__init__(self,master)
self.master.title("Window")
self.master.geometry("420x75")
self.grid(padx=10,pady=10)
self.CreateWidgets()
def CreateWidgets(self):
self.lbText = Label(self, text="Would you like to play \"Find the secret letter?\"", font="Verdana")
self.lbText.grid(row=0, column=0, columnspan = 3)
self.button = Button(self, text="Yes, please", command=self.next, background = "green")
self.button.grid(row=1, column=0)
self.btnDisplay = Button(self, text="No, thanks", command = self.window, background = "red")
self.btnDisplay.grid(row=1, column=1)
def window(self):
self.tl = Toplevel(self)
self.master.destroy()
def next(self):
self.lbText.grid_remove()
self.button.grid_remove()
self.btnDisplay.grid_remove()
self.lbText = Label(self, text="You're currently playing \"Find the secret letter\"", font="Verdana")
self.lbText.grid(row=0, column=0, columnspan = 10)
self.username = Label(self, text="Enter your username")
self.username.grid(row=1, column=0)
self.enText = Entry(self)
self.enText.grid(row=1, column=2, columnspan=5)
self.ok = Button(self, text="OK", command = self.wordguess)
self.ok.grid(row=1, column=8)
def wordguess(self):
global username
self.master.geometry("420x90")
username = self.enText.get()
self.username.grid_remove()
self.enText.grid_remove()
self.word = Label(self, text="You have {0} points, enter a word?".format(50))
self.word.grid(row=1, column=0)
self.entry = Entry(self)
self.entry.grid(row=1, column=1, columnspan=4)
self.ok = Button(self, text="OK", command = self.numright)
self.ok.grid(row=1, column=8)
def numright(self):
global points
global is_there
word = self.entry.get()
word = word.lower()
is_there = word.count(letter)
self.lbText.grid_remove()
self.word.grid_remove()
self.entry.grid_remove()
self.enText.grid_remove()
self.ok.grid_remove()
if is_there > 4:
self.master.geometry("200x70")
self.done = Label(self, text = "Congradulations, you have won!")
self.done.grid(row = 0, column = 0)
self.ok = Button(self, text="OK", command = self.window)
self.ok.grid(row=1, column=0)
else:
if (is_there < 5) and (is_there > 0):
points = points - 5
else:
points = points - 10
self.lbText = Label(self, text="You're currently playing \"Find the secret letter\"", font="Verdana")
self.lbText.grid(row=0, column=0, columnspan = 10)
self.numright = Label(self, text="That word has {} secret letters".format(is_there))
self.numright.grid(row=2, column=0)
self.word = Label(self, text="You have {0} points, enter a word?".format(points))
self.word.grid(row=1, column=0)
self.entry = Entry(self)
self.entry.grid(row=1, column=1, columnspan=4)
def scores(self):
output = open("names.txt", "a")
output.write("{0:2} {1} \n".format(str(points), username))
output.close()
output = open("names.txt", "r")
if __name__ == "__main__":
guiFrame = GUIFramework()
guiFrame.mainloop()
Your problem is the lines (in the else clause of the numright method):
self.numright = Label(self, text="That word has {} secret letters".format(is_there))
self.numright.grid(row=2, column=0)
self.numright is a method in your GUIFramework class, which you stomp on here and reassign it to a Label field. Also you probably have to put the self.ok button back onto the page since you grid_removed it above. I added the lines:
self.ok = Button(self, text="OK", command = self.numright)
self.ok.grid(row=1, column=8)
to the end of the numright method to put the OK button back. I then fiddled about by making a different method which just called self.numright which is how I caught the reassignment bug.