multiple commands for a tkinter button - python-3.x

I have been trying to execute 2 commands in 1 button. I have read that using lambda can solve the problem. But my situation is a little different. On button press, one command is to destroy the existing GUI, and the second command, I want is to open another SERVER GUI. Below is my existing button functionality.
exit_button = Button(topFrame, text='Quit', command=window.destroy)
How can I open another GUI using same button?
Thank you for any help.
Edit:-
I have now created the below function:
def close_func():
os.kill(os.getpid(), signal.SIGINT)
GUI_Interface()
window.destroy()
server_socket.close()
GUI_Interface is a function that I need to call after closing the existing .py file. If I put GUI_Interface as the first command for close_func(), then it really does not go back to the 2nd step and never closes the existing.py file.
And if I place GUI_Interface in the end, it just closes the existing one and nevr opens the function of the new .py file

Three options at least:
using or (with lambda if arguments):
from tkinter import Tk, Button
root = Tk()
Button(root, text='Press', command=lambda: print('hello') or root.destroy() or print('hi')).pack()
root.mainloop()
important to use or because and didn't work (don't know why or how this works at all)
or use function definitions:
from tkinter import Tk, Button
def func():
print('hello')
root.destroy()
print('hi')
root = Tk()
Button(root, text='Press', command=func).pack()
root.mainloop()
or lists with lambda:
from tkinter import Tk, Button
def func():
print('hello')
root.destroy()
print('hi')
root = Tk()
Button(root, text='Press', command=lambda: [print('hello'), root.destroy()]).pack()
root.mainloop()

Related

Calling functions without closing the master window - Python Tkinter

I've tried to create a software. In this software there is a menu widget with function button that open functions e widgets. But, I've noticed, to keep going on script, it's necessary to close the master window (menu).
I've created an example to you understand my problem.
from tkinter import *
#Create Fuction that open new fuctions and widget
def test():
#Open a new widget
def fun_test_1():
top_level = Tk()
def test1():
top_level1 = Toplevel()
top_level1.title('new1')
top_level1.mainloop()
Button(top_level, text='test1',command=test1).pack()
top_level.mainloop()
fun_test_1()
#Before, open the second widget
def fun_test_2():
print('def fun_test_2(): works!')
top_level = Tk()
def test1():
top_level1 = Toplevel()
top_level1.title('new1')
top_level1.mainloop()
Button(top_level, text='Button', command=test1).pack()
top_level.mainloop()
fun_test_2()
root = Tk()
root.title('MASTER')
Button(root, text='Button',command=test).pack()
root.mainloop()
So, I need that fun_test_2() be called without close the root widget
And all functions i've tried to change Tk() to Toplevel() and Toplevel() to Tk().
The problem is you calling mainloop more than once, and for creating more than one instance of Tk. When you call mainloop, it won't return until that window has been destroyed. That's a fundamental aspect of how tkinter works.
The solution is to not create more than one instance of Tk and to not call mainloop more than once. If you need multiple windows, create instances of Toplevel. And again, only call mainloop once in total, not once per window.

how to run a pyton script with button in gui tkinter

I am trying to run a python script(loop to listen to incoming email) with tkinter GUI, but the problem is when I executed the GUI my script is also run with the GUI even the button not pushed yet pls any help also if possible a little help with a button to stop my script, already thanks
the script used https://stackoverflow.com/a/59538076/13780184
my gui code:
import tkinter as tk
from tkinter import ttk
from tkinter import *
from itertools import chain
import os
# this is the function called when the button is clicked
def btnClickFunction():
print('clicked')
#the script is pasted here
# this is the function called when the button is clicked
def btnClickFunction():
print('clicked2')
# this is a function to get the user input from the text input box
def getInputBoxValue():
userInput = tInput.get()
return userInput
root = Tk()
# This is the section of code which creates the main window
root.geometry('618x400')
root.configure(background='#FFEFDB')
root.title('Hello, I\'m the main window')
# This is the section of code which creates a button
Button(root, text='Button text!', bg='#FFEFDB', font=('arial', 12, 'normal'), command=btnClickFunction).place(x=31, y=67)
# This is the section of code which creates the a label
Label(root, text='this is a label', bg='#FFEFDB', font=('arial', 12, 'normal')).place(x=43, y=151)
# This is the section of code which creates a button
Button(root, text='Button text!2', bg='#FFEFDB', font=('arial', 12, 'normal'), command=btnClickFunction).place(x=203, y=73)
# This is the section of code which creates a text input box
tInput=Entry(root)
tInput.place(x=251, y=200)
root.mainloop()

How to forget buttons created by a loop?

I need to create buttons with a for loop, here its an example of what i need to do:
But the problem is that when I press the different buttons, its prints the correct number, but it only forgets the last button created (in this case the button "4").
How can i do to forget all the buttons at once by only pressing one of them?
Its important the creation of the buttons by the loop
import tkinter as tk
root=tk.Tk()
def Eliminate(Number):
def Forget(number):
button.pack_forget()
print(number)
for i in range(Number):
button= tk.Button(root,text=i,command=lambda number=i:Forget(number))
button.pack()
Eliminate(5)
root.mainloop() ```
You need to pass in the button widget itself, not the number that created it. To do that you simply need to issue the command argument in a new line, like this:
import tkinter as tk
def Forget(btn):
btn.pack_forget()
def Eliminate(Number):
for i in range(Number):
button= tk.Button(root,text=i)
button.config(command=lambda button=button:Forget(button))
button.pack()
root=tk.Tk()
Eliminate(5)
root.mainloop()
However as the command is only calling the pack_forget method, it's much easier to forget making your own callback function and just provide pack_forget:
import tkinter as tk
def Eliminate(Number):
for i in range(Number):
button= tk.Button(root,text=i)
button.config(command=button.pack_forget)
button.pack()
root=tk.Tk()
Eliminate(5)
root.mainloop()

Connecting two files or classes in Python

I have two pages(each having a class) that that needs to be connected, page1(file1.py) and page2(file2.py).
I want to open page2 with a button that will be on page1. As mentioned the codes for each page are in separate files, file1 and file2.
I used import file2 but as soon as I run the program it opens and runs file2.py which I dont want. I want to open page2 via button when the user needs to open it.
Any suggestions???
Regards
Khisrow
Just put the second page inside a class, import that inside a function and call that. Then give the function as the command to a Button.
Like this:
Module_one:
import tkinter as tk
def test_func():
import Module_two
Module_two.TestClass()
root = tk.Tk()
b = tk.Button(root, text="Click", command=lambda: test_func())
b.pack()
root.mainloop()
Module_two:
class TestClass:
def __init__(self):
import tkinter as tk
root = tk.Toplevel()
lbl = tk.Label(root, text="Test Label")
lbl.pack()

Enter button doesn't trigger

i am trying to open a google search window using python and the enter button trigger doesn't work.
so if you press the enter button it should open the window but it doesn't.
i am also using tkinter for gui.
help me :( thanks
(i am using win 10)
import tkinter as tk
def keyup():
opener()
def opener():
import webbrowser
text = name.get().strip()
query=str(text)
webbrowser.open("www.google.com/search?rlz=1C1CHZL_koUS766US766&ei=rDxSWtPFOeKt0gK4_YioDg&q="+query)
def clear():
name.delete(0, 'end')
root = tk.Tk()
ler=tk.Label(root, text = "what do you want to search?",font=("Helvetica", 40))
ler.pack()
name = tk.Entry(root,width=100)
name.pack()
widget=tk.Button(root, text = 'search', command =opener,width=30,height=3)
widget.pack()
widget.bind('<Enter>', opener)
wider=tk.Button(root, text = 'clear', command =clear,width=10,height=3)
wider.pack()
root.mainloop()
how do i fix this?
You have few mistakes
for key "Enter" is event <Return>. You can also assign <Return> to root and it works even when button is not selected/focused.
root.bind('<Return>', opener)
or assign to Entry
name.bind('<Return>', opener)
and "Enter" will run opener only when Entry is focused.
command= executes function without arguments but bind() executes function with one argument so you have to define function with argument which has default value and then it will work with both.
def opener(event=None):
use http:// in url because you can use file:// to open local file. On Linux link without http:// is treated as local file.
Smaller mistakes: put all import at top to make code more readable, Entry returns string so you don't need str()
EDIT: added name.bind('<Return>', opener)
import tkinter as tk
import webbrowser
def opener(event=None):
text = name.get().strip()
webbrowser.open("http://www.google.com/search?q="+text)
def clear():
name.delete(0, 'end')
root = tk.Tk()
ler=tk.Label(root, text="what do you want to search?", font=("Helvetica", 40))
ler.pack()
name = tk.Entry(root,width=100)
name.pack()
name.bind('<Return>', opener) # added
widget=tk.Button(root, text='search', command=opener, width=30, height=3)
widget.pack()
widget.bind('<Return>', opener)
wider = tk.Button(root, text='clear', command=clear, width=10, height=3)
wider.pack()
root.mainloop()
With Tk, the event binding <Enter is used to describe the cursor moving over the space occupied by the widget (entering the space). The binding you wish to use is <Return>, which is mapped to the return (enter) key on the keyboard.
I think it should be <Return> instead of <Enter>. Also, the indentations seem incorrect.

Resources