How do I get certain buttons working? - python

How do I get a button working?
from tkinter import *
window=Tk()
window.title=title("Test")
button=button(window, text='Increase variable', command=Var)
button.pack()
variable=0
def Var():
variable=variable+1
I get an error saying Var is not defined. If I put the function before the button code I get an error when clicking the button.
from tkinter import *
variable=0
def Var():
variable=variable+1
window=Tk()
window.title=title("Test")
button=button(window, text='Increase variable', command=Var)
button.pack()
Clicking on the button will yield an error local variable referenced before assignment. How do I get this to work?

If variable is a global variable, you need to indicate to Var that this is the case.

Related

Why my button in tkinter don't works normal [duplicate]

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 last year.
I wanted to make button in tkinter, but when I started program, the command always calls when code just starts.
Here is example code:
import tkinter as tk
from tkinter import messagebox
window = tk.Tk()
window.title("Why this don't works???")
window.wm_geometry("100x100")
def message():
messagebox.showinfo("Hi there")
button = tk.Button(text="Hello", command=message())
button.grid(column=0, row=0)
while True:
window.update()
And then, button didn't worked. (When you press it, it don't works.)
I don't know what I'm doing wrong, so I need help.
The command should be a pointer to a function
In the code you wrote, the command gets the return value from the function.
command=message()
The correct way is
command = message
The problem is you are requesting a return value from the fucnction. Try using this.
from tkinter import *
# import messagebox from tkinter module
import tkinter.messagebox
# create a tkinter root window
root = tkinter.Tk()
# root window title and dimension
root.title("When you press a button the message will pop up")
root.geometry('75x50')
# Create a messagebox showinfo
def onClick():
tkinter.messagebox.showinfo("Hello World!.", "Hi I'm your message")
# Create a Button
button = Button(root, text="Click Me", command=onClick, height=5, width=10)
# Set the position of button on the top of window.
button.pack(side='top')
root.mainloop()
You have 2 errors:
first:
It must be command=message
second:
You must give a message argument too, you entered a title only.
Or, what you can do is.
Add another variable.
command = message()
Before this line,
button = tk.Button(text="Hello", command=message())
And chande this line to,
button = tk.Button(text="Hello", command=command)

Properly assigning a function with arguments to a button in Tkinter

I have two files:
functions.py
import tkinter as tk
class funcs():
def func1(entry):
entry.delete(0, tk.END)
main.py
import tkinter as tk
import functions as f
root = tk.Tk()
entrybox = tk.Entry(master=root).grid()
button = tk.Button(master=root, command=f.funcs.func1(entrybox)).grid()
root.mainloop()
In main.py, I have assigned the command func1 with the argument entrybox to the widget button.
My intent is to have the entry argument represent an entry widget I want to manipulate.
This line of code is broken:
button = tk.Button(master=root, command=f.funcs.func1(entrybox)).grid()
The problem is that when I run the program, the function is called immediately and does not get assigned to the button.
I am looking for a way to assign a function with arguments to a button in tkinter.
You can use an anonymous function:
tk.Button(
master=root,
command=lambda: f.funcs.func1(entrybox)
)
Python recognizes the broken line of code as an immediate call, so you have to do lambda:
button = tk.Button(master=root, command=lambda: f.funcs.func1(entrybox)).grid()
and as far as I know that will get rid of the problem.

Python Tkinter pop-up not being displayed

I am trying to get a Tkinter popup to display when a button is clicked.My issue is that every thing runs just fine except the popup will not produce. I have tried multiple ways to create the popup using tkMessagebox and Toplevel() but still not luck. The program runs but when the button is click nothing happens. I have referenced similar post but still can not find the issue in my code. Any thoughts?
from tkinter import *
def new():
root2 = Tk()
root2.geometry('250x250')
l = Label(root2,text="Please Scan Tag").pack()
root2.mainloop()
# setting main frame
root = Tk()
root.geometry('800x650')
root.title("Pass")
root.configure(background= "white")
label_0 = Label(root, text="Pass",width=10,font=("bold", 50),fg= "green",bg="white")
label_0.place(x=186,y=76)
Button(root,command="new", text='new',font=
("bold",15),width=15,height=4,bg='blue',fg='white').place(x=155,y=300)
root.mainloop()
The command option requires a reference to a callable function, not a string.
Button(root,command=new, ...)

Global and local variables in function combined with tkinter

Why is this possible? When I click on the button it switches to "I have been clicked". However, "action" is a local object of the function. How can the function cause a global change?
I'd expected that I need to set action global in the function via "global action".
import tkinter as tk
from tkinter import ttk
#Creates tk object
win = tk.Tk()
win.title("TEST")
alabel = ttk.Label(win, text = "A Label")
alabel.grid(column=0, row=0)
#function
def click_me():
action.configure(text="I have been clicked")
alable.configure(foreground='red')
alable.configure(text='a red label')
#adding a button
action = ttk.Button(win, text="Click me", command = click_me)
action.grid(column=1, row=0)
win.mainloop()
However, "action" is a local object of the function.
That is incorrect. Until you assign something to action within the function, it refers to the global variable action.
Because you are calling a method on action rather than setting action to a new value, you are modifying the object that action points to.

OUTPUT textbox to a window

I tried to execute the following code in my system, and the window does not respond if the submit button is clicked
import Tkinter as tk
from Tkinter import *
top=Tk()
text=Text(top)
def onsubmit():
a=v.get()
ea.textbox(text=a)
v=StringVar()
t=Entry(top, textvariable=v)
submit=Button(top,text="SUBMIT",command=onsubmit)
t.grid(row=0,column=0)
submit.grid(row=0,column=1)
text.grid(row=1,column=0)
top.mainloop()
If you run your script from terminal or just look at the text output of your program in some other way you will see the following error (just after pressing the button):
NameError: global name 'ea' is not defined
The error is on the second line of onsubmit function. Here is the working version:
def onsubmit():
a=v.get()
text.insert(INSERT, a)

Categories