Related
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 created one window and split it into 3 different windows. I want to add a clock to each screen, that does not depend on the other clocks.
my code opens 2 window- one is the timer and the second one is the window that i split to 3 windows.
from tkinter import *
import Tkinter as tk
class split_screen(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.label = tk.Label(self, text="", width=10,fg="black", bg ="red", font="david 18 bold underline")
self.label.pack()
self.remaining = 0
self.countdown(1000)
self.configure(background="black")
def screen(self):
root = Tk()
root.geometry("650x700")
root.configure(bg='black')
root.title("test")
left = Frame(root, borderwidth=200, relief="solid")
right = Frame(root, borderwidth=20, relief="solid")
box3 = Frame(right, borderwidth=2, relief="solid")
box1 = Frame(left, borderwidth=2, relief="solid")
box2 = Frame(left, borderwidth=2, relief="solid")
label1 = Label(box3, text="winner's" + "\n\n\n" + "Player 1",fg= "black", bg = "red", font = "david 18 bold underline")
label2 = Label(box1, text="Computer 1",fg = "black", bg = "red", font= "david 18 bold underline")
label3 = Label(box2, text="Computer 2",fg = "black", bg = "red", font= "david 18 bold underline")
left.pack(side="left", expand=True, fill="both")
right.pack(side="right", expand=True, fill="both")
box3.pack(expand=True, fill="both", padx=10, pady=10)
box1.pack(expand=True, fill="both", padx=10, pady=10)
box2.pack(expand=True, fill="both", padx=10, pady=10)
label1.pack()
label2.pack()
label3.pack()
def countdown(self, remaining = None):
if remaining is not None:
self.remaining = remaining
if self.remaining <= 0:
self.label.configure(text="time's up!")
else:
self.label.configure(text="%d" % self.remaining)
self.remaining = self.remaining - 1
self.after(1000, self.countdown)
if __name__ == "__main__":
app = split_screen()
app.screen()
app.mainloop()
I did multiple changes :
As Bryan Oakley said, don't create multiple instances of Tk(). You also don't need to import tkinter twice.
import tkinter as tk
class split_screen(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.geometry("650x700")
self.configure(bg='black')
self.title("test")
left = tk.Frame(self, borderwidth=200, relief="solid")
right = tk.Frame(self, borderwidth=20, relief="solid")
box1 = tk.Frame(left, borderwidth=2, relief="solid")
box2 = tk.Frame(left, borderwidth=2, relief="solid")
box3 = tk.Frame(right, borderwidth=2, relief="solid")
label1 = tk.Label(box3, text="winner's" + "\n\n\n" + "Player 1",fg= "black", bg = "red", font = "david 18 bold underline")
label2 = tk.Label(box1, text="Computer 1",fg = "black", bg = "red", font= "david 18 bold underline")
label3 = tk.Label(box2, text="Computer 2",fg = "black", bg = "red", font= "david 18 bold underline")
clock1 = Clock(box1, 1000)
clock2 = Clock(box2, 2000)
clock3 = Clock(box3, 1300)
left.pack(side="left", expand=True, fill="both")
right.pack(side="right", expand=True, fill="both")
box3.pack(expand=True, fill="both", padx=10, pady=10)
box1.pack(expand=True, fill="both", padx=10, pady=10)
box2.pack(expand=True, fill="both", padx=10, pady=10)
label1.pack()
label2.pack()
label3.pack()
class Clock():
def __init__(self, frame, count):
self.frame = frame
self.label = tk.Label(frame, text="", width=10,fg="black", bg ="red", font="david 18 bold underline")
self.label.pack()
self.remaining = 0
self.countdown(count)
frame.configure(background="black")
def countdown(self, remaining = None):
if remaining is not None:
self.remaining = remaining
if self.remaining <= 0:
self.label.configure(text="time's up!")
else:
self.label.configure(text="%d" % self.remaining)
self.remaining = self.remaining - 1
self.frame.after(1000, self.countdown)
if __name__ == "__main__":
app = split_screen()
app.mainloop()
The simplest solution -- and arguably the best -- is to create a class that represents a single timer. You can then make as many instances as you want. This is precisely the sort of thing that classes are for: to encapsulate some behavior inside an object.
Since you're using a label to display the time, you can either have the timer class inherit from Label, or you can pass the label in when creating the timer.
Here's an example which inherits from tk.Label:
import tkinter as tk
class TimerLabel(tk.Label):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.after_id = None
self.remaining = None
def countdown(self, seconds):
if seconds <= 0:
self.configure(text="Time's up!")
else:
self.configure(text=f"{seconds}")
self.after_id = self.after(1000, self.countdown, seconds-1)
def stop(self):
if self.after_id:
self.configure(text="cancelled")
self.after_cancel(self.after_id)
Using the above class definition, you can create as many timer widgets as you want. Here's an example that creates three:
root = tk.Tk()
timer1 = TimerLabel(root, width=10)
timer2 = TimerLabel(root, width=10)
timer3 = TimerLabel(root, width=10)
timer1.pack(side="top", padx=20)
timer2.pack(side="top", padx=20)
timer3.pack(side="top", padx=20)
timer1.countdown(30)
timer2.countdown(20)
timer3.countdown(10)
root.mainloop()
Background: I am a newbie doing projects to get familiar with python.
Problem: I would like to add an "equal to" sign in my calculator I made using tkinter. How can I do this?
My idea: I commented out the attempt I made. Is there anyway to include if statement with an "and" so I can say something like if method is add and equal to then +
Here is my code:
from tkinter import Tk, Label, Button, IntVar, Entry, END, W, E
class Calculator:
def __init__(self, master):
self.total = 0
self.entered_number = 0
self.title_label = Label(master, text="Calculator")
self.total_label_text = IntVar()
self.total_label_text.set(self.total)
self.total_label = Label(master, textvariable=self.total_label_text)
self.label = Label(master, text="Total:")
vcmd = master.register(self.validate)
self.entry = Entry(master, validate="key", validatecommand=(vcmd, "%P"))
self.addition_button = Button(
master, text="Add", command=lambda: self.update("add")
)
self.subtract_button = Button(
master, text="Minus", command=lambda: self.update("minus")
)
self.equalto_button = Button(
master, text="Equals", command=lambda: self.update("equalto")
)
self.reset_button = Button(
master, text="Reset", command=lambda: self.update("reset")
)
self.close_button = Button(master, text="Close", command=master.quit, bg="red")
# LAYOUT
self.label.grid(row=1, column=1, sticky=W)
self.total_label.grid(row=1, column=0, columnspan=2, sticky=E)
self.entry.grid(row=2, column=0, columnspan=2, sticky=W + E)
self.addition_button.grid(row=3, column=0)
self.subtract_button.grid(row=3, column=1)
self.equalto_button.grid(row=3, column=2)
self.reset_button.grid(row=4, column=0, sticky=W + E)
self.close_button.grid(row=4, column=1)
def validate(self, new_text):
if not new_text:
self.entered_number = 0
return True
try:
self.entered_number = int(new_text)
return True
except ValueError:
return False
def update(self, method):
if method == "add":
self.total += self.entered_number
elif method == "minus":
self.total -= self.entered_number
# elif method == "equalto":
# self.total += self.entered_number
else:
self.total = 0
self.total_label_text.set(self.total)
self.entry.delete(0, END)
root = Tk()
my_gui = Calculator(root)
root.mainloop()
In Python, the "equal to" sign is "==", with modifications being made on the first "=" if you want "greater than", "less than", and etc.
Edit: "=" is an assignment whilst "==" is a comparison.
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.
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.