I am trying to code a CPS test for fun but I ran into a problem:
I defined a function at the beginning of the code and than I defined a tkinter button which has this function as command and the button should disappear in this function. When I do it like this it says that the variable of the button isn't defined yet but when I defined it vice versa so first the button and then the function it says that the function is not defined yet.
Can anyone help me?
(Sorry for my bad English)
import tkinter as tk
root = tk.Tk()
root.title("CPS test")
root.geometry("1000x1000")
root.configure(bg='white')
def getstring():
duration = entry.get()
entry.delete(0)
entry.place_forget()
okbutton.place_forget
lab1 = tk.Label(text = "How long should the CPS test last? 2,5 or 10 seconds?(Answer with 2,5,10)", font = "Ariel 20", bg = "#FFFFFF")
button = tk.Button(root, text = "Click me!", font = "Ariel 50", bg = "#FFFFFF", fg = "#000000")
entry = tk.Entry(root, font=('Arieal 20'), bd = 2)
okbutton = tk.Button(root, text = "OK", font = "Ariel 20", bg = "#FFFFFF", fg = "#000000", bd = 2, command = getstring())
lab1.place(x = 45,y = 250)
entry.place(x = 344, y = 350)
entry.insert(0, "Enter your answer here!")
okbutton.place(x = 465, y = 400)
root.mainloop()
I think the error is on the button name.
def getstring():
duration = entry.get()
entry.delete(0)
entry.place_forget()
button.place_forget
Just replace "okbutton" by "button" and it should work.
Else just declared the "okbutton" before the function
Related
This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 1 year ago.
The goal of this project is to have a user be able to enter a m and b value into a tkinter window and have a graph show up when a button is pressed. There is multiple windows. There is a click to start button, and a menu to select the type of graph. Currently I am just focusing on getting the linear graph working. When the linear window is reached it prompts the user to enter a m and b value (through entries).
When the graph button is pressed I have coded for it to print the m and b value out. As of right now I cannot get it to print out in the console. If anyone could help up let me know thanks!
Code:
from tkinter import *
root = Tk()
root.geometry("500x500")
class testInputs():
def __init__(self,m,b):
#self.displaygframe = Frame(root)
#self.displayglabe = Label(self.displaygframe,
#text="Test for now",
#font= "ComicSansMS 14")
print(m)
print(b)
class selectGraph(): #2
def __init__(self):
self.gselectframe = Frame(root)
self.gselectlabel = Label(self.gselectframe,
text = "Select Graph Type",
font= "ComicSansMS 14")
self.Var = IntVar()
#define buttons
self.linearButton = Radiobutton(self.gselectframe,
text="Linear",
variable = self.Var,
value=1)
self.quadraticButton = Radiobutton(self.gselectframe,text="Quadratic",
variable = self.Var,
value = 2)
self.confirmButton = Button(self.gselectframe, text = "Continue", command = self.transferAnswer,
fg = "red", font = "ComicSansMS 14",
bd = 2, bg = "light green")
self.sgBack = Button(self.gselectframe, text = "Back", command = self.backButton,
fg = "red", font = "ComicSansMS 5",
bd = 2, bg = "light green")
#pack
self.gselectlabel.pack()
self.linearButton.pack(anchor=W)
self.quadraticButton.pack(anchor=W)
self.confirmButton.pack()
self.gselectframe.pack()
mainloop()
def transferAnswer(self):
answer= self.Var.get()
print(str(answer))
self.gselectframe.destroy()
getEquation(answer)
def backButton(self):
self.gselectframe.destroy()
introWindow()
class getEquation(): #3
def __init__(self, answer):
if answer == 1:
self.linearInput()
if answer == 2:
self.quadraticInput()
else:
selectGraph()
def linearInput(self):
self.linearFrame = Frame(root)
#define widgets
self.linearLabel = Label(self.linearFrame,
text = "Enter Linear Equation",
font= "ComicSansMS 14")
self.linearBack = Button(self.linearFrame, text = "Back", command = self.backButtonLinear,
fg = "red", font = "ComicSansMS 5",
bd = 2, bg = "light green")
formatLabel = Label(self.linearFrame,
text="Format:",
font="ComicSansMS 14")
equationLabel = Label(self.linearFrame,
text="y=mx+b",
font="ComicSansMS 14",
fg="midnight blue")
#name = StringVar()
#name2 = StringVar()
self.mLabel = Label(self.linearFrame,
text= "m=",
font= "ComicSansMS 14")
self.mInput = Entry(self.linearFrame,
width =2, font="ComicSansMS 14")
self.bLabel = Label(self.linearFrame,
text = "b=",
font = "ComicSansMs 14")
self.bInput = Entry(self.linearFrame,
width=2,
font="ComicSansMS 14")
#get info from widget
m = self.mInput.get()
b = self.mInput.get()
self.graphButton = Button(self.linearFrame, text = "Graph", command = self.nextFromLinear(m,b), #error from here
fg = "red", font = "ComicSansMS 14",
bd = 2, bg = "light green")
#place widgets on screen
#self.linearFrame.pack()
self.linearLabel.grid(row=0,column=0)
formatLabel.grid(row=1,column=0)
equationLabel.grid(row=2, column=0)
self.bInput.grid(row=4, column=1)
self.bLabel.grid(row=4, column=0)
self.mInput.grid(row=3, column=1)
self.mLabel.grid(row=3,column= 0)
self.graphButton.grid(row=5, column=1) #error from here
self.linearBack.grid(row=5,column=0)
self.linearFrame.pack()
mainloop()
def quadraticInput(self):
self.quadraticFrame = Frame(root)
self.quadraticLabel = Label(self.quadraticFrame,
text = "Enter Quadratic Equation",
font= "ComicSansMS 14")
self.quadraticLabel.pack()
self.quadraticBack = Button(self.quadraticFrame, text = "Back", command = self.backButtonQuadratic,
fg = "red", font = "ComicSansMS 5",
bd = 2, bg = "light green")
self.quadraticBack.pack(anchor=SW)
self.quadraticFrame.pack()
mainloop()
def backButtonLinear(self):
self.linearFrame.destroy()
selectGraph()
def backButtonQuadratic(self):
self.quadraticFrame.destroy()
selectGraph()
def nextFromLinear(self,m,b):
#self.linearFrame.destroy()
#testInputs(m,b)
print(m)
print(b)
#Figuring out how to write singular back button function for all equation Frames:
#def backButton(self, frame1):
#frame1.destroy()
#selectGraph
class introWindow(): #1
def __init__(self):
self.introframe = Frame(root)
self.introlabel = Label(self.introframe,
text = "Graphing Program",font="ComicSansMS 14")
self.introbutton = Button(self.introframe, text = "Click to start", command = self.windowRemoval,
fg = "red", font = "ComicSansMS 14",
bd = 2, bg = "light green")
self.introlabel.pack()
self.introbutton.pack()
self.introframe.pack()
mainloop()
def windowRemoval(self):
self.introframe.destroy()
selectGraph()
introWindow()
The command argument to Button() must be a function reference, not a call to the function. You're calling the function when you create the button, not when it's clicked.
Use a lambda to create an anonymous function. And it has to call get() when it's called, you can't call it when creating the button, or you'll get the initial values of the inputs, not what the user has entered into them.
self.graphButton = Button(self.linearFrame, text = "Graph",
command = lambda: self.nextFromLinear(self.mInput.get(),self.bInput.get()), #error from here
fg = "red", font = "ComicSansMS 14",
bd = 2, bg = "light green")
I made this program in Tkinter in python where a small window pops up when the code is run and a start button would pop up and make the window full screen and show the content after. I want to make the button destroy itself after I press it so it makes a fullscreen and removes the button. I am still a beginner and would like the answer to be simple. The solution I am looking for is to maybe destroy the button completely(preferred) or move it way out of sight in the fullscreen window. Here is the code:
import Tkinter as w
from Tkinter import *
w = Tk()
w.geometry("150x50+680+350")
def w1():
w.attributes("-fullscreen", True)
l1 = Label(w, text = "Loaded!", height = 6, width = 8).pack()
global b1
b1.place(x = -10000, y = -10000)
b1 = Button(w, text = "Start", height = 3, width = 20, command = w1).place(x = 0, y = 10)
b2 = Button(w, text = "Exit", command = w.destroy).place(x = 1506, y = 0)
w.mainloop()
As you can see I want to make button one destroy itself.
Try this:
import tkinter as tk # Use `import Tkinter as tk` for Python 2
root = tk.Tk()
root.geometry("150x50+680+350")
def function():
global button_start
root.attributes("-fullscreen", True)
label = tk.Label(root, text="Loaded!", height=6, width=8)
label.pack()
button_start.place_forget() # You can also use `button_start.destroy()`
button_start = tk.Button(root, text="Start", height=3, width=20, command=function)
button_start.place(x = 0, y = 10)
button_exit = tk.Button(root, text="Exit", command=root.destroy)
button_exit.place(x=1506, y=0)
root.mainloop()
PS: Please read this.
Try:
b1.place_forget()
This will essentially "forget" about the button and hide it from view.
Edit:
If you are getting the error that b1 is None try:
b1 = Button(w, text = "Start", height = 3, width = 20, command = w1)
b1.place(x = 0, y = 10)
You need to add the b1.place() option at the bottom for this to work
The problem:
I am trying to update the same text widget box from a function that contains some text. Instead a whole new text window appears every time.
Here is my code:
from tkinter import *
import os
#Tkinter graphics
homepage = Tk()
homepage.title("My first GUI")
# set size of window
homepage.geometry('1200x400')
# Add image file
bg = PhotoImage(file = "maxresdefault.png")
# Show image using label
label1 = Label( homepage, image = bg)
label1.place(x = 0, y = 0)
label2 = Label( homepage, text = "Test App")
label2.pack()
# Create Frame
frame1 = Frame(homepage)
frame1.pack()
#button initatiors
def buttonupdate():
S = Scrollbar(homepage)
T = Text(homepage, height=100, width=30)
T.pack()
T.pack(side=RIGHT, fill= Y)
S.pack(side = RIGHT, fill = Y)
S.config(command=T.yview)
T.insert(END, "test")
T.config(yscrollcommand=S.set, state=DISABLED)
# Static buttons
tickets30button = Button(text = "This is button 1", command=buttonupdate)
tickets30button.place(x=0, y=26)
mcibutton = Button(text = "This is button 2")
mcibutton.place(x=0, y=52)
hdebutton = Button(text = "This is button 3")
hdebutton.place(x=0, y=78)
homepage.mainloop()
Here is the result if I click on the first button three times:
Let me know if you have any suggestions that I can try.
Thank you for your time,
I was able to update my text window instead of create a new one, upon each click of a button, thanks to #TheLizzard.
He mentioned to move the section of code that creates the text window outside of the function and keep the section of code that creates the text, inside the function.
Before:
#button initiators
def buttonupdate():
S = Scrollbar(homepage)
T = Text(homepage, height=100, width=30)
T.pack()
T.pack(side=RIGHT, fill= Y)
S.pack(side = RIGHT, fill = Y)
S.config(command=T.yview)
T.insert(END, "test")
T.config(yscrollcommand=S.set, state=DISABLED)
After: (UPDATED)
S = Scrollbar(homepage)
T = Text(homepage, height=100, width=30)
T.pack(side=RIGHT, fill= Y)
S.pack(side = RIGHT, fill = Y)
S.config(command=T.yview)
T.config(yscrollcommand=S.set, state=DISABLED)
#button initatiors
def myTicketstatusbutton():
T.delete(1.0,END)
T.insert(END, "test")
I am building a Python GUI where I can enter commands such as play music, pictures etc. which will be executed. I have not yet started working on OS codes.
How do I take user commands using the text box?
or, simply:
How do I make this text box a command box?
from tkinter import *
import quotes #Imported external module having quotations
my_quote = "Random quote."
root = None
textSpace = None
texter = None
def textBox(parent): #creates a text box
global textSpace
textSpace = Entry(parent)
textSpace.place(x = 205, y = 190, width = "200")
def text_input():
global texter
texter = textSpace.get()
print("Text entered:", texter)
def quotes(): #Random quote at bottom of window
global root
quote_text = Label(root, text = my_quote, font = ("papyrus", 7,
"italic"),
bg = "light sky blue")
quote_text.pack(side = "bottom")
def main_win(): #This is the main window
global root
global textSpace
input = textBox #I am not sure if this is correct
user_command = input
root = Tk() #We have to initiate the window right
root.title("Greetings")
root.configure(bg = "light sky blue")
root.geometry("600x350")
head_text = Label(root, text = "PEWDS", font=("Comic Sans MS", 36,
"bold"), bg = "light sky blue", fg = "purple")
head_text.pack(side = "top")
sub_text = Label(root, text = "Enter your command below.", font=("MS
sans serif", 20, "italic"), bg = "light sky blue", fg = "alice blue")
sub_text.pack()
quotes()
go_button = Button(text = "GO", command = text_input)
go_button.place(x=285, y=230)
go_button.configure(bg = "cadetblue")
textBox(root)
root.mainloop
main_win()
from tkinter import *
from tkinter import font
root = Tk()
root.title("Wills Gui")
root.geometry('1400x700')
#root.configure(background='black')
def P1C():
if B1(fg) :
B1.fg(fg = red)
B1["text"] = "Pin 2 Active"
B1["fg"] = "green"
else:
B1.fg(fg = green)
B1["text"] = "Pin 2 Inactive"
B1["fg"] = "red"
B1 = Button(root, height = 8, width = 15, font="Times 12 bold",
wraplength=80, command = P1C, text="| Q | B1 Inactive", bg="black",
fg="yellow").grid(row =0,column =0)
root.mainloop()
I have simplified the code i just need it to change the colour of the button text from green to red, etc every time it is pressed.
The error is right there in the trace
in P1C if B1(fg) : NameError: name 'fg' is not defined
The variable fg in the function is not defined prior to use.