Adding a scrollbar to a frame using Tkinter (Python) - python

I've a simple GUI thats shows the users some options, after putting the number of the initial options to be shown. In this case, 4:
By clicking on Add row you can add a row to the GUI. The thing is that if the user wants 100 options, the GUI becomes extra big and all the options are not shown.
So I would like to have a scrollbar only on the options space, not the rest parts. Sorry for the bad Photoshop, but I would like to have something like this:
The options space is the FrameTwo, so I would like to have the entire FrameTwo inside of scrollbar like the image above.
# -*- coding: utf-8 -*-
from Tkinter import *
import Image
import ImageTk
import tkFileDialog
import datetime
class Planificador(Frame):
def __init__(self,master):
Frame.__init__(self, master)
self.master = master
self.initUI()
def initUI(self):
self.master.title("Plan")
self.frameOne = Frame(self.master)
self.frameOne.grid(row=0,column=0)
self.frameTwo = Frame(self.master)
self.frameTwo.grid(row=1, column=0)
self.frameThree = Frame(self.master)
self.frameThree.grid(row=2, column=0)
# Borrar esto?
self.txt = Text(self)
self.txt.pack(fill=BOTH, expand=1)
self.piezastext = Label(self.frameOne, text = " Amount of pieces ", justify="center")
self.piezastext.grid(row=1, column=0)
self.entrypiezas = Entry(self.frameOne,width=3)
self.entrypiezas.grid(row=2, column=0, pady=(5,5))
self.aceptarnumpiezas = Button(self.frameOne,text="Click me", command=self.aceptar_piezas,width=8)
self.aceptarnumpiezas.grid(row=6, column=0, pady=(5,5))
def aceptar_piezas(self):
try:
val = int(self.entrypiezas.get())
self.aceptar_piezas_ok()
except ValueError:
showerror('Error', "Introduce un numero")
def aceptar_piezas_ok(self):
self.num_piezas = self.entrypiezas.get()
self.piezastext.grid_remove()
self.entrypiezas.grid_remove()
self.aceptarnumpiezas.grid_remove()
self.optionmenus_piezas = list()
self.numpiezas = []
self.numerolotes = []
self.optionmenus_prioridad = list()
self.lotes = list()
self.mispiezas = ['One', 'Two', 'Three', 'Four', 'Five']
self.n = 1
while self.n <= int(self.num_piezas):
self.textopieza = Label(self.frameTwo, text = "Pieza: ", justify="left")
self.textopieza.grid(row=self.n, column=0)
var = StringVar()
menu = OptionMenu(self.frameTwo, var, *self.mispiezas)
menu.config(width=10)
menu.grid(row=self.n, column=1)
var.set("One")
self.optionmenus_piezas.append((menu, var))
self.numpiezastext = Label(self.frameTwo, text = "Numero de piezas: ", justify="center")
self.numpiezastext.grid(row=self.n, column=2, padx=(10,0))
self.entrynumpiezas = Entry(self.frameTwo,width=6)
self.entrynumpiezas.grid(row=self.n, column=3, padx=(0,10))
self.entrynumpiezas.insert(0, "0")
self.textoprioridad = Label(self.frameTwo, text = "Prioridad: ", justify="center")
self.textoprioridad.grid(row=self.n, column=4)
var2 = StringVar()
menu2 = OptionMenu(self.frameTwo, var2, "Normal", "Baja", "Primera pieza", "Esta semana")
menu2.config(width=10)
menu2.grid(row=self.n, column=5)
var2.set("Normal")
self.optionmenus_prioridad.append((menu2, var2))
self.lotestext = Label(self.frameTwo, text = "Por lotes?", justify="center")
self.lotestext.grid(row=self.n, column=6, padx=(10,0))
self.var1 = IntVar()
self.entrynumlotes = Checkbutton(self.frameTwo, variable=self.var1)
self.entrynumlotes.grid(row=self.n, column=7, padx=(5,10))
self.lotes.append(self.var1)
self.numpiezas.append(self.entrynumpiezas)
self.n += 1
self.anadirpiezas = Button(self.frameThree, text="Add row", command=self.addpieza, width=10)
self.anadirpiezas.grid(row=0, column=2, pady=(10,10))
self.calculotext = Label(self.frameThree, text = "Other stuff ")
self.calculotext.grid(row=1, column=2, padx=(10,0), pady=(10,10))
self.graspbutton = Button(self.frameThree, text="OPT 1", width=10)
self.graspbutton.grid(row=2, column=1)
self.parettobutton = Button(self.frameThree, text="OPT 2",width=10)
self.parettobutton.grid(row=2, column=2, pady=(10,10), padx=(10,0))
self.parettoEvolbutton = Button(self.frameThree, text="OPT 2", width=10)
self.parettoEvolbutton.grid(row=2, column=3, pady=(10,10), padx=(10,0))
def addpieza(self):
self.textopiezanuevo = Label(self.frameTwo, text = "Pieza: ", justify="left")
self.textopiezanuevo.grid(row=int(self.num_piezas)+1, column=0)
var = StringVar()
menu = OptionMenu(self.frameTwo, var, *self.mispiezas)
menu.grid(row=self.n, column=1)
menu.config(width=10)
menu.grid(row=int(self.num_piezas)+1, column=1)
var.set("One")
self.optionmenus_piezas.append((menu, var))
self.numpiezastext = Label(self.frameTwo, text = "Numero de piezas: ", justify="center")
self.numpiezastext.grid(row=int(self.num_piezas)+1, column=2, padx=(10,0))
self.entrynumpiezas = Entry(self.frameTwo,width=6)
self.entrynumpiezas.grid(row=int(self.num_piezas)+1, column=3, padx=(0,10))
self.entrynumpiezas.insert(0, "0")
self.textoprioridad = Label(self.frameTwo, text = "Prioridad: ", justify="center")
self.textoprioridad.grid(row=int(self.num_piezas)+1, column=4)
var2 = StringVar()
menu2 = OptionMenu(self.frameTwo, var2, "Normal", "Baja", "Primera pieza", "Esta semana")
menu2.config(width=10)
menu2.grid(row=int(self.num_piezas)+1, column=5)
var2.set("Normal")
self.optionmenus_prioridad.append((menu2, var2))
self.lotestext = Label(self.frameTwo, text = "Por lotes?", justify="center")
self.lotestext.grid(row=int(self.num_piezas)+1, column=6, padx=(10,0))
self.var1 = IntVar()
self.entrynumlotes = Checkbutton(self.frameTwo, variable=self.var1)
self.entrynumlotes.grid(row=int(self.num_piezas)+1, column=7, padx=(5,10))
self.lotes.append(self.var1)
self.numpiezas.append(self.entrynumpiezas)
self.num_piezas = int(self.num_piezas)+1
if __name__ == "__main__":
root = Tk()
aplicacion = Planificador(root)
root.mainloop()
FrameOne is used to put an image I removed to make this example more simple. And FrameThree are the buttons you can see at the bottom of the GUI.
So it would be very helpful if someone could give me a hand and tell me how to put the entire FrameTwo inside of a scrollbar as you can see on the third image.
Thanks in advance.

One of your problems, is that "frameTwo" has no limits to it's size. If you don't limit it's size, any scrollbar you add, never will act. But limiting the size of you frame have the inconvenient of limit the number of lines you can grid, making the scrollbar useless again.
A solution is creating a frame inside the "frameTwo", to receive the pieces you create. This way, by one hand, allow you to limit the size of "frameTwo" and attach the scrollbar to it, and by other hand, allow you to grid the pieces you add to the frame located inside "frameTwo", named, let's say, "ListFrame". when "ListFrame" size becomes bigger than "frameTwo" size, you can now make the scrollbar work.
I change your code with the changes mentioned above. Check it out.
The changes are commented in the code.
Sorry for the short explanation, but i'm a bit hurry. I may edit this answer when I have more time.
PS: Sorry if my english is not the best
# -*- coding: utf-8 -*-
from Tkinter import *
import Image
import ImageTk
import tkFileDialog
import datetime
class Planificador(Frame):
def __init__(self,master):
Frame.__init__(self, master)
self.master = master
self.initUI()
def initUI(self):
self.master.title("Plan")
self.frameOne = Frame(self.master)
self.frameOne.grid(row=0,column=0)
self.frameTwo = Frame(self.master)
self.frameTwo.grid(row=1, column=0)
#Creating of a new frame, inside of "frameTwo" to the objects to be inserted
#Creating a scrollbar
#The reason for this, is to attach the scrollbar to "FrameTwo", and when the size of frame "ListFrame" exceed the size of frameTwo, the scrollbar acts
self.canvas=Canvas(self.frameTwo)
self.listFrame=Frame(self.canvas)
self.scrollb=Scrollbar(self.master, orient="vertical",command=self.canvas.yview)
self.scrollb.grid(row=1, column=1, sticky='nsew') #grid scrollbar in master, but
self.canvas['yscrollcommand'] = self.scrollb.set #attach scrollbar to frameTwo
self.canvas.create_window((0,0),window=self.listFrame,anchor='nw')
self.listFrame.bind("<Configure>", self.AuxscrollFunction)
self.scrollb.grid_forget() #Forget scrollbar because the number of pieces remains undefined by the user. But this not destroy it. It will be "remembered" later.
self.canvas.pack(side="left")
self.frameThree = Frame(self.master)
self.frameThree.grid(row=2, column=0)
# Borrar esto?
self.txt = Text(self)
self.txt.pack(fill=BOTH, expand=1)
self.piezastext = Label(self.frameOne, text = " Amount of pieces ", justify="center")
self.piezastext.grid(row=1, column=0)
self.entrypiezas = Entry(self.frameOne,width=3)
self.entrypiezas.grid(row=2, column=0, pady=(5,5))
self.aceptarnumpiezas = Button(self.frameOne,text="Click me", command=self.aceptar_piezas,width=8)
self.aceptarnumpiezas.grid(row=6, column=0, pady=(5,5))
def AuxscrollFunction(self,event):
#You need to set a max size for frameTwo. Otherwise, it will grow as needed, and scrollbar do not act
self.canvas.configure(scrollregion=self.canvas.bbox("all"),width=600,height=500)
def aceptar_piezas(self):
#IMPORTANT!!! All the objects are now created in "ListFrame" and not in "frameTwo"
#I perform the alterations. Check it out
try:
val = int(self.entrypiezas.get())
self.aceptar_piezas_ok()
self.scrollb.grid(row=1, column=1, sticky='nsew') #grid scrollbar in master, because user had defined the numer of pieces
except ValueError:
showerror('Error', "Introduce un numero")
def aceptar_piezas_ok(self):
self.num_piezas = self.entrypiezas.get()
self.piezastext.grid_remove()
self.entrypiezas.grid_remove()
self.aceptarnumpiezas.grid_remove()
self.optionmenus_piezas = list()
self.numpiezas = []
self.numerolotes = []
self.optionmenus_prioridad = list()
self.lotes = list()
self.mispiezas = ['One', 'Two', 'Three', 'Four', 'Five']
self.n = 1
while self.n <= int(self.num_piezas):
self.textopieza = Label(self.listFrame, text = "Pieza: ", justify="left")
self.textopieza.grid(row=self.n, column=0)
var = StringVar()
menu = OptionMenu(self.listFrame, var, *self.mispiezas)
menu.config(width=10)
menu.grid(row=self.n, column=1)
var.set("One")
self.optionmenus_piezas.append((menu, var))
self.numpiezastext = Label(self.listFrame, text = "Numero de piezas: ", justify="center")
self.numpiezastext.grid(row=self.n, column=2, padx=(10,0))
self.entrynumpiezas = Entry(self.listFrame,width=6)
self.entrynumpiezas.grid(row=self.n, column=3, padx=(0,10))
self.entrynumpiezas.insert(0, "0")
self.textoprioridad = Label(self.listFrame, text = "Prioridad: ", justify="center")
self.textoprioridad.grid(row=self.n, column=4)
var2 = StringVar()
menu2 = OptionMenu(self.listFrame, var2, "Normal", "Baja", "Primera pieza", "Esta semana")
menu2.config(width=10)
menu2.grid(row=self.n, column=5)
var2.set("Normal")
self.optionmenus_prioridad.append((menu2, var2))
self.lotestext = Label(self.listFrame, text = "Por lotes?", justify="center")
self.lotestext.grid(row=self.n, column=6, padx=(10,0))
self.var1 = IntVar()
self.entrynumlotes = Checkbutton(self.listFrame, variable=self.var1)
self.entrynumlotes.grid(row=self.n, column=7, padx=(5,10))
self.lotes.append(self.var1)
self.numpiezas.append(self.entrynumpiezas)
self.n += 1
self.anadirpiezas = Button(self.frameThree, text="Add row", command=self.addpieza, width=10)
self.anadirpiezas.grid(row=0, column=2, pady=(10,10))
self.calculotext = Label(self.frameThree, text = "Other stuff ")
self.calculotext.grid(row=1, column=2, padx=(10,0), pady=(10,10))
self.graspbutton = Button(self.frameThree, text="OPT 1", width=10)
self.graspbutton.grid(row=2, column=1)
self.parettobutton = Button(self.frameThree, text="OPT 2",width=10)
self.parettobutton.grid(row=2, column=2, pady=(10,10), padx=(10,0))
self.parettoEvolbutton = Button(self.frameThree, text="OPT 2", width=10)
self.parettoEvolbutton.grid(row=2, column=3, pady=(10,10), padx=(10,0))
def addpieza(self):
self.textopiezanuevo = Label(self.listFrame, text = "Pieza: ", justify="left")
self.textopiezanuevo.grid(row=int(self.num_piezas)+1, column=0)
var = StringVar()
menu = OptionMenu(self.listFrame, var, *self.mispiezas)
menu.grid(row=self.n, column=1)
menu.config(width=10)
menu.grid(row=int(self.num_piezas)+1, column=1)
var.set("One")
self.optionmenus_piezas.append((menu, var))
self.numpiezastext = Label(self.listFrame, text = "Numero de piezas: ", justify="center")
self.numpiezastext.grid(row=int(self.num_piezas)+1, column=2, padx=(10,0))
self.entrynumpiezas = Entry(self.listFrame,width=6)
self.entrynumpiezas.grid(row=int(self.num_piezas)+1, column=3, padx=(0,10))
self.entrynumpiezas.insert(0, "0")
self.textoprioridad = Label(self.listFrame, text = "Prioridad: ", justify="center")
self.textoprioridad.grid(row=int(self.num_piezas)+1, column=4)
var2 = StringVar()
menu2 = OptionMenu(self.listFrame, var2, "Normal", "Baja", "Primera pieza", "Esta semana")
menu2.config(width=10)
menu2.grid(row=int(self.num_piezas)+1, column=5)
var2.set("Normal")
self.optionmenus_prioridad.append((menu2, var2))
self.lotestext = Label(self.listFrame, text = "Por lotes?", justify="center")
self.lotestext.grid(row=int(self.num_piezas)+1, column=6, padx=(10,0))
self.var1 = IntVar()
self.entrynumlotes = Checkbutton(self.listFrame, variable=self.var1)
self.entrynumlotes.grid(row=int(self.num_piezas)+1, column=7, padx=(5,10))
self.lotes.append(self.var1)
self.numpiezas.append(self.entrynumpiezas)
self.num_piezas = int(self.num_piezas)+1
if __name__ == "__main__":
root = Tk()
aplicacion = Planificador(root)
root.mainloop()

You have option menus, labels, entry boxes, and checkbuttons in FrameTwo. How would you expect a scrollbar for the entire frame would work? Would it scroll all widgets at the same time? Tkinter scrollbars are generally attached to listboxes, canvases, entry widgets, and text fields. http://effbot.org/zone/tkinter-scrollbar-patterns.htm Come up with a simple example that illustrates the problem for further help.

Related

Parameters passed to a function does not works as intended

When the "view" button is pressed, it should trigger the function solution(i) such that label should be displayed in the new window. The problem is that the window opens and the previous label is packed but the label which gets it's text from "i" does not gets packed, Is there any issue in passing the parameter.
Any help is appreciated.
root = Tk()
root.config(background = "#303939")
root.state('zoomed')
def pre():
with open("DoubtSolution.txt","r+") as f:
dousol = f.read()
dousol_lst = dousol.split("`")
k = 0
window = Tk()
window.config(background = "#303939")
window.state('zoomed')
predoubt = Label(window,
text="Previous Doubts",
fg="Cyan",
bg="#303939",
font="Helvetica 50 bold"
).grid(row=0, column=1)
def solution(text):
print(text)
window1 = Tk()
window1.config(background="#303939")
window1.state('zoomed')
sol = Label(window1,
text=text[:text.find("~")],
font=font.Font(size=20),
bg="#303939",
fg="Cyan")
sol.pack()
window1.mainloop()
for i in dousol_lst:
if i[-5:] == admno:
doubt = Label(window, text=i[i.find("]]")+2:i.find("}}}}")], font=font.Font(size=20), bg="#303939",
fg="Cyan")
doubt.grid(row=2+k, column=1, pady=10)
view = Button(
master=window,
text="View", font=font.Font(size=15, family="Helvetica"),
activebackground="White",
bg="Teal",
bd=0.8,
fg="White",
command = lambda k = k:solution(i)
)
view.grid(row=2+k, column=2, padx=20)
k=k+1
window.mainloop()
previous = Button(
master=root,
text="Previous Doubts", font="Helvetica 22 bold",
activebackground="White",
bg="Teal",
bd=0.8,
fg="White",
command = pre
).grid(row=4, column=3, padx=20)
root.mainloop()

Python: How can I remove excel entries via openpyxl using a feedback loop?

I am building a small app to study the vocabulary of various languages (see below the code for Mandarin). I have the basic funcions which work well. Now I want to add a button in my GUI where i can remove entries (i.e. individual words) from the database, once i have mastered the word (i.e. a button in tkinter which would remove the entry). After removing, the random function in python should then only select words from the reduced database. Do you have any idea how to do this? Any help is welcome!
from tkinter import *
import random
import sys
import os
randvalue_start = random.randint(2, 592)
window = Tk()
window.title('Mandarin Vocabulary')
window.geometry('500x400')
icon = PhotoImage(file = r'C:\Users\PycharmProjects\Mandarin\HSKlogopng.600px.png')
icon2 = icon.subsample(5 ,5)
label1 = Label(window, image = icon2, anchor="ne")
import openpyxl
path = r"\Users\PycharmProjects\Mandarin\characters.xlsx"
worbook = openpyxl.load_workbook(path, read_only=True)
sheet = worbook.active
row_count = (sheet.max_row)
def english_btn1():
global randvalue_start
english = f"B{randvalue_start}"
english_value = sheet[english].value
label_eng = Label(window, text=english_value+":", width=20, height=3, font=("TkDefaultFont",15))
label_pin = Label(window, text="", font=30, width=15, height=3)
label_mand = Label(window, text="", font=30, width=15, height=4)
label_eng.grid(row=8,column=1,rowspan=2)
label_pin.grid(row=8, column=2)
label_mand.grid(row=9, column=2)
def pinying_btn2():
global randvalue_start
pinying = f"C{randvalue_start}"
mandarin = f"D{randvalue_start}"
pinying_value = sheet[pinying].value
mandarin_value = sheet[mandarin].value
combined = f"{pinying}'/'{mandarin}"
combined_value = f"{pinying_value}'/'{mandarin_value}"
label_pin = Label(window ,text=pinying_value,font=("TkDefaultFont",15), width=10, height=2)
label_mand = Label(window,text=mandarin_value,font=("TkDefaultFont",30), width=8,height=2, borderwidth=5,relief="ridge")
label_pin.grid(row=8,column=2)
label_mand.grid(row=9,column=2)
randvalue_start = random.randint(2, 592)
def reset():
os.execl(sys.executable, sys.executable, *sys.argv)
frame = LabelFrame(window,text="Input",padx=5, pady=5)
frame.grid(row=0,column=1,padx=10,pady=10)
btn2 = Button(frame,text = "Show answer", fg = "green" ,width=20 ,command=pinying_btn2)
btn3 = Button(frame,text = "clear", fg= "red", width=20 ,command=reset)
btn1 = Button(frame,text = "Next character", fg = "black" ,width=20,command=english_btn1)
words_label = Label(frame,text = "# of characters: " + str(row_count))
label1.grid(row=0,column=2, columnspan=2)
Label(window, text="", width=20, height=3, font=("TkDefaultFont", 15)).grid(row=8, column=1)
label_blk1 = Label(window, text="", font=("TkDefaultFont", 15), width=10, height=3).grid(row=8, column=2)
label_blk2 = Label(window, text="", font=("TkDefaultFont", 30), width=10, height=2, padx=1, pady=1).grid(row=9, column=2)
btn1.grid(padx=5, pady=5)
btn2.grid(padx=5, pady=5)
btn3.grid(padx=5, pady=5)
words_label.grid(padx=5,pady=5)
window.mainloop()
This is not really a question about Pyxl or Excel. The question is really: how do you generate random numbers that skip certain numbers.
from random import randint
n = 592
numbers = list(range(n))
# To get a random number from the list
random_number = numbers[randint(0, len(numbers)-1)]
# To remove 123 from the list
numbers.remove(123)

python tkinter entry input divided by label input from optionMenu

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)

Tkinter Add/Remove "windows" within current window

Basically below is what I have right now. The problem is that each of these buttons opens a new window and I would prefer that in the top right I have the current function such as add course shown and then when I click the "add time" the add course would be removed and the add time be shown. Is there a way I could do this for each of the "editing" dict operations and also have an "up-to-date" table printed in the bottom right of this window? Thanks.
class menu():
def __init__(self, master):
self.master = master
self.table = {}
self.createButtons()
# creates buttons with alignment and their functions
def createButtons(self):
load = Button(self.master, text = "Load Table",
command = self.loadTable)
load.grid(row =0, column =0, pady = 30)
course = Button(self.master, text = "Add Course",
command = self.addCourse)
course.grid(row =1, column =0, pady = 30)
time = Button(self.master, text = "Add Time",
command = self.addTime)
time.grid(row =2, column =0, pady = 30)
reset = Button(self.master, text = "Reset Time",
command = self.resetTime)
reset.grid(row =3, column =0, pady = 30)
comment = Button(self.master, text = "Change Comment",
command = self.changeComment)
comment.grid(row =4, column =0, pady = 30)
view = Button(self.master, text = "View Table",
command = self.viewTable)
view.grid(row =5, column =0, pady = 30)
def addCourse(self):
#creates addCourse window and text boxes for input
toplevel = Toplevel()
toplevel.title('Add Course')
Label (toplevel, text='Enter the course name').grid()
courseBox = Entry(toplevel, width=10)
courseBox.grid()
label =Label (toplevel, text='Enter the hours per week spent on course')
label.grid(padx=10,pady=10)
weekHoursBox = Entry(toplevel, width=10)
weekHoursBox.grid()
#function to accept input from boxes into dict
def callback():
course = courseBox.get()
weekHours = weekHoursBox.get()
string = "0 "+ str(weekHours) + " "
self.table[course] = string
enterB = Button(toplevel, text='Enter Information', command = callback)
enterB.grid(pady=10)
Working example - I hope it is what you expected.
from Tkinter import *
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.master = master
self.table = {}
self.rightFrame = None # for first "grid_forget()"
self.createButtons()
#-----------------------------
def run(self):
self.master.mainloop()
#-----------------------------
# creates buttons with alignment and their functions
def createButtons(self):
self.menuFrame = Frame(self.master)
self.menuFrame.grid(row=0, column=0)
load = Button(self.menuFrame, text="Load Table", command=self.loadTable)
load.grid(row=0, column=0, pady=30)
course = Button(self.menuFrame, text="Add Course", command=self.addCourse)
course.grid(row=1, column=0, pady=30)
time = Button(self.menuFrame, text="Add Time", command=self.addTime)
time.grid(row=2, column=0, pady=30)
reset = Button(self.menuFrame, text="Reset Time", command=self.resetTime)
reset.grid(row=3, column=0, pady=30)
comment = Button(self.menuFrame, text="Change Comment", command=self.changeComment)
comment.grid(row=4, column=0, pady=30)
view = Button(self.menuFrame, text="View Table", command=self.viewTable)
view.grid(row=5, column=0, pady=30)
#-----------------------------
def loadTable(self):
# remove previous content
if self.rightFrame:
self.rightFrame.grid_forget()
# create new content
self.rightFrame = Frame(self.master)
self.rightFrame.grid(row=0, column=1, sticky=N)
title = Label(self.rightFrame, text='load Table')
title.grid(row=0, column=0)
#-----------------------------
def addCourse(self):
# remove previous content
if self.rightFrame:
self.rightFrame.grid_forget()
# create new content
self.rightFrame = Frame(self.master)
self.rightFrame.grid(row=0, column=1, sticky=N)
#creates addCourse window and text boxes for input
title = Label(self.rightFrame, text='Enter the course name')
title.grid(row=0, column=0)
courseBox = Entry(self.rightFrame, width=10)
courseBox.grid(row=1, column=0)
label = Label (self.rightFrame, text='Enter the hours per week spent on course')
label.grid(row=2, column=0)
weekHoursBox = Entry(self.rightFrame, width=10)
weekHoursBox.grid(row=3, column=0)
#function to accept input from boxes into dict
def callback():
course = courseBox.get()
weekHours = weekHoursBox.get()
string = "0 "+ str(weekHours) + " "
self.table[course] = string
enterB = Button(self.rightFrame, text='Enter Information', command=callback)
enterB.grid(row=4, column=0)
#-----------------------------
def addTime(self):
# remove previous content
if self.rightFrame:
self.rightFrame.grid_forget()
# create new content
self.rightFrame = Frame(self.master)
self.rightFrame.grid(row=0, column=1, sticky=N)
title = Label(self.rightFrame, text='Add Time')
title.grid(row=0, column=0)
#-----------------------------
def resetTime(self):
# remove previous content
if self.rightFrame:
self.rightFrame.grid_forget()
# create new content
self.rightFrame = Frame(self.master)
self.rightFrame.grid(row=0, column=1, sticky=N)
title = Label(self.rightFrame, text='Reset Time')
title.grid(row=0, column=0)
#-----------------------------
def changeComment(self):
# remove previous content
if self.rightFrame:
self.rightFrame.grid_forget()
# create new content
self.rightFrame = Frame(self.master)
self.rightFrame.grid(row=0, column=1, sticky=N)
title = Label(self.rightFrame, text='Change Comment')
title.grid(row=0, column=0)
#-----------------------------
def viewTable(self):
# remove previous content
if self.rightFrame:
self.rightFrame.grid_forget()
# create new content
self.rightFrame = Frame(self.master)
self.rightFrame.grid(row=0, column=1, sticky=N)
title = Label(self.rightFrame, text='View Table')
title.grid(row=0, column=0)
#----------------------------------------------------------------------
Application(Tk()).run()

Erase widgets from window

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.

Categories