window label is not printing : Python - python

I want to create two windows.
Behaviour of windows:
Window1 has a label and a button. When I click on that button, 2nd window has to open. 2nd window have a label.
Problem:
Label in 2nd window is not appearing.
Code:
def window1():
root = tkinter.Tk()
root.geometry("200x200")
root.title("Window1")
var = tkinter.StringVar()
tkinter.Label(root, textvariable = var, bg = "red").grid(row = 0, column = 0)
var.set("This is window1")
tkinter.Button(root, text = "Button1", command = OnBut).grid(row = 0, column = 1)
root.mainloop()
def OnBut():
window2()
def window2():
root = tkinter.Tk()
root.title("Window2")
root.geometry("250x250")
var = tkinter.StringVar()
tkinter.Label(root, textvariable = var, bg = "blue").grid(row = 1, column = 0, padx = 3, pady = 3)
tkinter.Button(root, text = "Button", command = OnBut).grid(row = 0, column = 1, padx =3, pady = 3)
var.set("This is window2") #not appearing <-- problem
root.mainloop()
window1()
when I call window2 seperately, its working fine. Why label not printing in 2nd window, by clicking on button?

You don't really need a real function for your command in this case. This is what lambdas are made for -- callbacks!
Remove your onBut function (which is the problem anyway, since root isn't defined there) and replace your command in each button with:
command = lambda: window2(root)
Currently, when you call onBut, it tries to do:
window2(root)
# HELP I DON'T KNOW WHAT root IS!!
This throws a NameError on my copy. Your code may vary.
Since you're editing willy nilly, let me just write you some working code.
import tkinter
def run():
root = tkinter.Tk()
root.title("Window1")
s_var = tkinter.StringVar()
tkinter.Label(root, textvariable = s_var).pack()
tkinter.Button(root, text = "Button", command = lambda: makewindow(root)).pack()
s_var.set("Window #1")
def makewindow(root):
top = tkinter.Toplevel(root)
top.title("Window2")
s_var = tkinter.StringVar()
tkinter.Label(top, textvariable = s_var).pack()
tkinter.Button(top, text = "Button", command = lambda: makewindow(root)).pack()
s_var.set("Window #2")
if __name__ == "__main__":
run()

Related

How to close a tkinter dialog box after running another function?

I am working with python tkinter and I have a dialog box window that pops up. I have two buttons in the box:
from tksheet import Sheet
from tkinter import *
import os
import sys
import mapMaker2
root=Tk()
root.title('Map Tool')
root.geometry("750x750")
sheetframe = Frame(root)
sheetframe.grid(row = 0, column = 0,)
buttonEditlabel = Button(sheetframe, text='Edit Labels', width=12, command=lambda: [openEditWindow()], bg='#cacccf',fg='black')
buttonEditlabel.grid(row = 0, sticky=W, column = 0, pady = (25,5), padx = (50,0))
def openEditWindow():
top = Toplevel(root)
top.geometry("260x195")
top.title('Edit Axes Labels')
frm = Frame(top, borderwidth=0, relief='ridge')
frm.grid(row = 0, column = 0, pady = (20,0),padx=(20,0))
b_cancel = Button(frm, text='Close', width=10)
b_cancel['command'] = top.destroy
b_cancel.grid(column = 0, row = 6, pady = (15,0),padx=(0,0))
b_save = Button(frm, text='Save', width=10)
b_save['command'] = lambda: editLabels()
b_save.grid(column = 1, row = 6, sticky = E, pady = (15,0),padx=(0,0))
def editLabels():
pass
mainloop()
Cancel button closes the window with top.destroy command. I would like the Save button to also close the window after running the editLabels() function first. I have tried:
b_save['command'] = [lambda: editLabels(), top.destroy]
but this doesn't work.
Here is one way you can do it. Create a function to destroy the top window.
def kill_main():
top.destroy()
top.update()
Then call the function wherever you want. You don't have to add kill_main() to the button itself. Just put it inside the next function you are opening so that it will close the Top windows first and then run the rest of the editlabels() function. Hopefully, it makes sense to you.
def editLabels():
kill_main()
pass

Python entry to store input into a variable stores nothing, returns initial value

Ok, so basic story. I have created an entry. After you introduce text, you have to click a button to store the inputted text into a variable that is later printed.
Here is the code:
from Tkinter import *
def myClick(entry_name, var):#defines function to get input from entry and store into var
var = entry_name.get()
root = Tk()#creates initial tk
lbl1 = Label(root, text = "hello")#before entry label
lbl1.grid(row = 0, column = 0)#label griding
ent = Entry(root, width = 15)# the entry
ent.grid(row = 1, column = 0)#entry gridding
hello = None #variable to store entry input
bt1 = Button(root, command = myClick(ent, hello))#button 1 creation and function attribution
bt1.grid(row = 3, column = 0)#button 1 griding
root.mainloop()
print(hello)
It is very unclear to me why the function does not get the input from the entry.
bt1 = Button(root, command = myClick(ent, hello))
In this line, you call myClick function with parameters, not just pass it. That means that myClick is executed once after the module is launched and then it does nothing. If you want to print the entry input, I recommend you do the following:
from tkinter import *
root = Tk()
lbl1 = Label(root, text="hello")
lbl1.grid(row=0, column=0)
ent = Entry(root, width=15)
ent.grid(row=1, column=0)
def myClick():
var = ent.get()
print(var)
bt1 = Button(root, command=myClick)
bt1.grid(row=3, column=0)
root.mainloop()
Also code after root.mainloop() doesn't excecute.
just define a normal function :
from tkinter import *
def blinta():
var = ent.get()
ent.delete(0,END)
print(var)
root = Tk()#creates initial tk
lbl1 = Label(root, text = "hello")#before entry label
lbl1.grid(row = 0, column = 0)#label griding
ent = Entry(root, width = 15)# the entry
ent.grid(row = 1, column = 0)#entry gridding
bt1 = Button(root, command = blinta)
bt1.grid(row = 3, column = 0)
root.mainloop()
This will work I'm sure.

Run two Commands when you hit a Tkinter Button

I would like a Tkinter button to clear my current Grid, and also go to a function, and I cannot think of how to do it. I have a grid that is a menu, and in another function I have the code for what was just opened by hitting the button.
in short I want a button, when clicked to do this: self.studyGuide and this: self.frame.grid_forget.
Here is my code:
from tkinter import *
class App:
def __init__(self,master):
frame = Frame(master)
frame.grid()
self.sg = Button(frame, text = "Study Guide", command = self.buttonStart, fg="red")
self.sg.grid(row = 2, column = 1)
self.quizlet = Button(frame, text = "Quizlet", command = self.quizlet)
self.quizlet.grid(row = 2, column = 2)
self.flashcard = Button(frame, text = "Flash Cards", command = self.flashcard)
self.flashcard.grid(row = 2, column = 3)
self.quitButton = Button(frame, text = "Quit", command = frame.quit)
self.quitButton.grid(row = 3, column = 2)
self.text = Label(frame, text = "Social Studies Study Tool")
self.text.grid(row = 1, column = 2)
def buttonStart(frame):
self.studyGuide()
self.frame.grid_forget()
def studyGuide(self):
studyGuide = Frame()
studyGuide.pack()
self.sgText = Label(studyGuide, text = "This is not real.")
self.sgText.pack()
def quizlet(self):
print("Quizlet")
def flashcard(self):
print("Flashcards")
root = Tk()
app = App(root)
root.mainloop()
Simply, have the callback passed to the Button constructor call the other 2 functions:
def foo(self):
self.studyGuide()
self.frame.grid_forget()
root = Tk()
my_button = Button(root, text="I'm doing stuff", command=foo)

Empty Tkinter Window

I am doing a bit of basic Tkinter code, and when I launch I get no errors, but my window is empty, even though I have added things to them. I saw this question here, but that does not help me, as I have what it says to do.
from tkinter import *
class App:
def __init__(self,master):
frame = Frame(master)
frame.pack
self.sg = Button(frame, text = "Study Guide", command = self.studyGuide)
self.sg.grid(row = 2, column = 1)
self.sg.pack()
self.quizlet = Button(frame, text = "Quizlet", command = self.quizlet)
self.quizlet.grid(row = 2, column = 2)
self.quizlet.pack()
self.flashcard = Button(frame, text = "Flash Cards", command = self.flashcard)
self.flashcard.grid(row = 2, column = 3)
self.flashcard.pack()
self.quitButton = Button(frame, text = "Quit", command = frame.quit)
self.quitButton.grid(row = 3, column = 2)
self.quitButton.pack()
self.text = Label(frame, text = "Social Studies Study Tool")
self.text.grid(row = 1, column = 2)
self.text.pack()
def studyGuide(self):
print("Study Guide")
def quizlet(self):
print("Quizlet")
def flashcard(self):
print("Flashcards")
root = Tk()
app = App(root)
root.mainloop()
First, for every element that you call grid for, don't call pack. You only need to use one or the other. Second:
frame = Frame(master)
frame.pack
You appear to be missing a parentheses here.
frame = Frame(master)
frame.pack()
Don't mix up layout managers! Use either pack() or grid(), but not both.
If you use pack, add the side where to pack the items:
frame.pack() # note the missing () in your code
...
self.sg.pack(side=TOP)
If you use grid(), add frame.grid() to the top of your code:
frame.grid()
...
self.sg.grid(row = 2, column = 1)

get previous Entry

I am trying to make a tkinter widget that will remember the previous entry. The problem I am having is the that the button method I made erases the previous entry every time its pressed.
from Tkinter import *
class step1():
def __init__(self):
pass
def getTextbook(self):
temp = str(textbook.get())
textbook.delete(0, END)
x = " "
x += temp
print x
def equal_button(self):
print getTextbook(self)
root = Tk()
root.title("step1")
root.geometry("600x600")
s = step1()
app = Frame(root)
app.grid()
label = Label(app, text = "step1")
label.grid()
textbook = Entry(app, justify=RIGHT)
textbook.grid(row=0, column = 0, columnspan = 3, pady = 5)
textbook2 = Entry(app, justify=RIGHT)
textbook2.grid(row=1, column = 0, columnspan = 3, pady = 5)
button1 = Button(app, text = "Return", command = lambda: s.getTextbook())
button1.grid()
button2 = Button(app, text="Equal")
button2.grid()
root.mainloop()
The variable X in your getTextbook() is being overwritten every time you set it to " ". Try a list instead, and append each entry to the list when the button is pressed:
from Tkinter import *
root = Tk()
textbookList = []
def getTextbook():
textbookList.append(textbook.get())
textbook.delete(0,END)
print textbookList
textbook = Entry(root)
textbook.pack()
btn1 = Button(root, text='Return', command=getTextbook)
btn1.pack()
root.mainloop()

Categories

Resources