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()
Related
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()
First of all, thanks for stop by in my post. I'm a python amateur writer and I'm working in a GUI to capture some customer information, the first step is users make sure system is ON before they execute my application, for this, I use a messagebox.showinfo command.
However, this command also opens a secondary window (not sure why). Searching on internet, I realize that the best way to give ride of is with the instruction "withdraw".
my GUI should have a picture, which I use "Photoimage" for this but for some reason, the picture I'm trying to allocate into my GUI it goes to the secondary window open by messagebox.showinfo.
Does any one know why my picture ends on the secondary window and not in my main GUI?
the code below you can use in your computer you may need to create a folder in C and load a picture so you can see what I'm seeing here.
Thanks for stopping by.
from tkinter import *
import tkinter.messagebox
from tkinter import messagebox
import tkinter as tk
import os
################ To Avoid Parasite Window To Open ##################
##root = tkinter.Tk()
##root.withdraw()
messagebox.showinfo(message="Make sure System is ON", title="My App 1.00")
def configure():
print ("hi")
####################### Main Window Design ##########################
##
#####################################################################
main = tkinter.Tk()
main.resizable(0,0)
main.geometry('490x510+700+50')
main.title('My App 1.00')
Label(main, text='GSYSTEM 1.00', font=("Tahoma", 20)).place(x=5,y=0)
LabelFrame(main, text=' Enter Data: ', font=("Tahoma", 14), height=180, width=480, bd=2, relief='ridge' ).place(x=5,y=100)
######################### PN ######################################
tmudir = "C:\\System"
os.chdir(tmudir)
Label(main, text='Scan Product :',font=("Tahoma", 12)).place(x=10,y=150)
LabelFrame(main, font=("Tahoma", 10), height=30, width=180, bd=3, relief='ridge').place(x=110,y=147)
pn_label = PhotoImage(file='TNL.png')
Label(image=pn_label).place(x=310,y=120)
####################### Main Window Design JO #######################
##
#####################################################################
LabelFrame(main, text=' ORDER INFO: ', font=("Tahoma", 14), height=100, width=480, bd=2, relief='ridge' ).place(x=5,y=360)
Label(main, text='Scan Order:',font=("Tahoma", 12)).place(x=10,y=385)
joborderinfo = Entry(main,font=("Arial Narrow", 12))
joborderinfo.place(x=265,y=385)
########## Main Window Design Buttons Start and Config ##############
##
#####################################################################
power_supply_btn = Button(main, text="START TEST", font=("Tahoma", 21), height=1, width=24, command=configure)
power_supply_btn.place(x=55,y=40)
power_supply_btn.config(state=NORMAL)
mainloop()
SystemExit()
You have to create the main window first, and thee use withdraw() to show only the message. You would do it like this:
import tkinter as tk
from tkinter import messagebox
main = tk.Tk()
main.withdraw()
messagebox.showinfo(message="Make sure System is ON", title="My App 1.00")
main.deiconify() # undo the withdraw() command.
def configure():
print ("hi")
main.resizable(0,0)
main.geometry('490x510+700+50')
main.title('My App 1.00')
# rest of your code
I have one menu in my GUI. This menu is used to select which label will be added to the GUI.
When I start the program, the menu already shows the default option selected, but this option isn't used anywhere in the program. It requires user's action (click and select in menu) to get something out of that menu.
I want my program to immediately use the default menu's option, that later can be changed by the user.
Can you give me tips not only for this specific label-related task, but in general? How to use the default menu value in the program, without interaction with the menu?
This is my code:
from tkinter import *
root=Tk()
root.title("test")
# root.geometry("400x400")
def selected(event):
myLabel=Label(root, text=clicked.get()).pack()
options=["a","b","c"]
clicked = StringVar()
clicked.set(options[0])
drop=OptionMenu(root,clicked,*options, command=selected)
drop.pack()
root.mainloop()
An easy way:
from tkinter import *
root=Tk()
root.title("test")
# root.geometry("400x400")
def selected(event):
myLabel['text'] = clicked.get()
options=["a","b","c"]
clicked = StringVar()
clicked.set(options[0])
drop=OptionMenu(root,clicked,*options, command=selected)
drop.pack()
myLabel = Label(root, text=clicked.get())
myLabel.pack()
root.mainloop()
Or I suggested you use textvariable,then you needn't to use a function to change the label:
from tkinter import *
root=Tk()
root.title("test")
# root.geometry("400x400")
options=["a","b","c"]
clicked = StringVar()
clicked.set(options[0])
drop=OptionMenu(root,clicked,*options)
drop.pack()
myLabel = Label(root, textvariable=clicked) # bind a textvariable
myLabel.pack()
root.mainloop()
from tkinter import *
def printSomething():
inputValue=textBox.get("1.0","end-1c")
res=response(inputValue)
label = Label(root, text=res)
#this creates a new label to the GUI
label.pack()
root = Tk()
button = Button(root, text="Print Me", command=printSomething)
button.pack()
textBox=Text(root, height=2, width=10)
textBox.pack()
root.mainloop()
I have written a python code that returns text. and print that in tkinter label.while i try to execute it shows "None" in label.
It would probably be better to create the label in the global namespace once and then just update the label every time you press the button.
I also recommend using import tkinter as tk vs from tkinter import * as it provides better maintainability as your code grows and you do not end up overwriting built in methods.
I have updated your code and changed a few things to better fit the PEP8 standard.
import tkinter as tk
def print_something():
label.config(text=text_box.get("1.0", "end-1c"))
root = tk.Tk()
tk.Button(root, text="Print Me", command=print_something).pack()
text_box = tk.Text(root, height=2, width=10)
text_box.pack()
label = tk.Label(root)
label.pack()
root.mainloop()
Just changing your line:
res = response(inputValue)
to
res = inputValue
worked for me, creating a new label every time I pressed the button.
this is my code so far o cant add buttons with out it creating more windows
////////
#import tkinter
import tkinter
#import tkmessagebox(buttons)
from tkinter import *
#create a new window
window = tkinter.Tk()
#title <------ put it before .mainloop
window.title("yeahh boiiii")
#window size
window.geometry("500x500")
#set a window icon
window.iconbitmap('N:\downloads\icon.ico.ico')#<---- 8bit file name
master = Tk()
def callback():
print ("click!")
b = Button(master, text="OK", command=callback)
b.pack()
#draws the window
window.mainloop()
////////
please help
Your problem is that you create 2 instances of Tk(). This is a bad idea, and you don't need to do it since you can make your button a child of the window object:
# Import tkinter
import tkinter as tk
# Create a new window
window = tk.Tk()
# Title <------ put it before .mainloop
window.title("yeahh boiiii")
# Window size
window.geometry("500x500")
# Set a window icon
window.iconbitmap('N:\downloads\icon.ico.ico') #<---- 8bit file name
def callback():
print ("click!")
b = tk.Button(window, text="OK", command=callback)
b.pack()
# Draw the window
window.mainloop()
I also rewrote your tkinter import, because you were importing it twice...