Run two Commands when you hit a Tkinter Button - python

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)

Related

Getting error when trying to create tkinter frame?

I'm trying to understand the following example below:
import sys
from tkinter import *
def btn():
login_frame.destroy()
home_frame = home()
home_frame.pack(fill="both", expand=True)
def btn2():
home_frame.destroy()
#home_frame = home()
#home_frame.pack(fill="both", expand=True)
def login():
frame = Frame(root)
Label(frame, text = "test1").grid(row = 0)
Label(frame, text = "test2").grid(row = 1)
Label(frame, text = "test3").grid(row = 2)
e1 = Entry(frame)
e2 = Entry(frame)
e1.grid(row=1, column = 1)
e2.grid(row=2, column = 1)
Button(frame, text = 'btn1', command = btn).grid(row = 4, column = 0, sticky = W, pady = 4)
Button(frame, text = 'btn2', command = btn).grid(row = 4, column = 1, sticky = W, pady = 4)
return frame
def home():
frame = Frame(root)
Label(frame, text="Welcome").pack()
Button(frame, text = 'test', command = btn2).pack()
return frame
root = Tk()
root.wm_title('test')
login_frame = login()
login_frame.pack(fill="both", expand=True)
root.mainloop()
I create the first login_frame, and btn1 destroys it and creates home_frame right? but when I click the next btn I get the following error:
NameError: name 'home_frame' is not defined
Why isn't this frame defined? I thought it would have been defined when btn is pushed and home_frame = home() ?

Ending a Functions Tkinter window from another Function

from tkinter import *
root = Tk()
root.title("Tournament")
root.geometry("360x100")
def Choice():
second = Toplevel()
second.title("Tournament")
second.geometry("360x100")
command = root.withdraw()
typechoice = Label(second, text = "Are you part of a Team or Solo?")
typechoice.grid(row=0,column=0)
solo1 = Button(second, text = "Solo", command = Soloscreen)
solo1.grid(row=1,column=0)
team1 = Button(second, text = "Team", command = Teamscreen)
team1.grid(row=2,column=0)
def Soloscreen():
third = Toplevel()
third.title("Tournament")
third.geometry("360x100")
typechoice = Label(third, text="Enter your name")
typechoice.grid(row=0, column=0)
done = Button(third, text="Done")
done.grid(row=1, column=0)
def Teamscreen():
fourth = Toplevel()
fourth.title("Tournament")
fourth.geometry("360x100")
typechoice = Label(fourth, text="Enter your name")
typechoice.grid(row=0, column=0)
done = Button(fourth, text="Done")
done.grid(row=1, column=0)
# Creating GUI widgets
Start = Button(root, text="Start",width = "30", command=Choice, fg="red")
PHS = Button(root, text="Previous High Scores", width = "30", fg="red")
ENQ = Button(root, text="Enter New Questions", width = "30", fg="red")
# Displaying them on screen
Start.grid(row = 1, column = 3)
PHS.grid(row = 2, column = 3)
ENQ.grid(row = 3, column = 3)
root.mainloop()
As you can see here I have a pretty simple multiple window set up currently, what I dont understand is that I'm able to close the Root window with command = root.withdraw()however under the Soloscreen and Teamscreen windows the same command but changed to close the Choice screen wont seem to work?

Tkinter - inserting information from a text box to a specific location in a python script

I've recently finished a data standardisation script and am currently trying to make it more user-friendly by creating an app with Tkinter. I have already managed to run the data standardisation script through Tkinter, but the script requires minor changes between different data sets.
What I'm trying to achieve is inserting a user-defined piece of text to a specific location in the script. I have tried the text widget on Tkinter, however I have only managed to open the script in the app, which is something I avoid doing (optimally the app user would not even need to see the original code).
What I'm rather trying to do is having a Tkinter textbox, with a 'Run' button next to it. That way when a user inserts a specific name (e.g. 'Law Conference Attendees Jan 2020') it would automatically place this piece of text here df['Data Identifier'] = ''
My current Tkinter code looks like this:
def __init__(self):
super(Root, self).__init__()
self.title("Python Tkinter Dialog Widget")
self.minsize(320, 200)
self.text_area = Text()
self.text_area.grid(column = 2, row = 3)
self.labelFrame = ttk.LabelFrame(self, text = "Open File")
self.labelFrame.grid(column = 0, row = 1, padx = 20, pady = 20)
self.button()
self.button1()
self.button2()
self.textbox()
self.textbox1()
self.textbox2()
def button(self):
self.button = ttk.Button(self.labelFrame, text = "Browse a File",command = self.open_file)
self.button.grid(column = 1, row = 1)
def button1(self):
self.button1 = ttk.Button(self.labelFrame, text = "Cleanse Campaign Codes",command = self.standardize)
self.button1.grid(column = 1, row = 7)
def button2(self):
self.button2 = ttk.Button(self.labelFrame, text = "Cleanse Data",command = self.helloCallBack)
self.button2.grid(column = 1, row = 8)
def textbox(self):
self.textbox = ttk.Entry(self.labelFrame)
self.textbox.grid(column = 6, row = 1)
def textbox1(self):
self.textbox1 = ttk.Entry(self.labelFrame)
self.textbox1.grid(column = 6, row = 2)
def textbox2(self):
self.textbox2 = ttk.Entry(self.labelFrame)
self.textbox2.grid(column = 6, row = 3)
def helloCallBack(self):
os.system('python data_cleansing_final.py')
def open_file(self):
open_return = filedialog.askopenfile(initialdir = "C:/", title="Select file to open", filetypes=(("python files", "*.py"), ("all files", "*.*")))
for line in open_return:
self.text_area.insert(END, line)
def standardize(self):
open_return = open_return.apply(lambda x: difflib.get_close_matches(x, textbox)[0])
root = Root()
root.mainloop()
I would very much appreciate any help or advice.
You could add a StringVar to your textbox.
self.inputstring = ttk.StringVar(self.lableFrame, value = 'value')
self.textbox2 = ttk.Entry(self.labelFrame, textvariable = self.inputstring)
self.textbox2.grid(column = 6, row = 3)
To read the variable you should use:
self.inputstring.get()
You can make an entry:
text = Entry(root, width=10)
text.grid(column=0, row=0)
With a button next to it:
run = Button(root, text="Run", width=10, command=runClicked)
run.grid(column=1, row=0)
And then a method called runClicked above these:
def runClicked():
userText = text.get()
And then the variable userText variable will hold whatever the user types in and you can use it as you wish.
All in all your code would look something like:
def runClicked():
userText = text.get()
text = Entry(root, width=10)
text.grid(column=0, row=0)
run = Button(root, text="Run", width=10, command=runClicked)
run.grid(column=1, row=0)

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