Tk-Inter messagebox secondary window error - python-3.x

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

Related

Turtle circle shape in a main GUI TK

First of all, thanks for stopping by into my post. I'm a amateur Python writer. Right now, I'm writing a code that will control some Agilent instruments, into my GUI I need to display a virtual LED that can show to the operator the current test progress status, like, yellow = testing, green = passed, red= failed.
I found that turtle module can easily create a circle by adding turtle.shape('cicle'), please see below a turtle code I have:
I want to make it clear, I did not wrote the below code, I found the code in this webpage.
from turtle import Screen, Turtle
CURSOR_SIZE = 30
def blink():
pen, fill = turtle.color()
turtle.color(fill, pen)
screen.ontimer(blink, 500) # 1/4 second blink
screen = Screen()
turtle = Turtle()
turtle.hideturtle()
turtle.shape('circle')
turtle.shapesize(50 / CURSOR_SIZE)
turtle.color('red', 'Yellow')
turtle.showturtle()
blink()
So, I want to place the button into my main GUI window:
########## Main Window Design##########
main = Tk()
main.geometry('860x500+65+0')
main.title('Firmware Upgrade System 1.0')
Label(main,text='Firmware Upgrade System 1.0', font=("Tahoma", 20)).place(x=0,y=0)
##! UUT 1 ####################################
LabelFrame(main, text=' UUT 1 INFORMATION : ', font=("Tahoma", 10), height=120, width=420, bd=2, relief='groove' ).place(x=10,y=40)
LabelFrame(main, text='Serial Number',font=("Tahoma", 10), height=47, width=110, bd=3, relief='ridge').place(x=13,y=60)
LabelFrame(main, text='Firmware Version',font=("Tahoma", 10), height=47, width=119, bd=3, relief='ridge').place(x=126,y=60)
def fw_upgrade():
print("pas")
blink()
#######Buttons and Functions
Button(main, text="FIRMWARE UPGRADE", font=("Tahoma", 12), height=1, width=20, command=fw_upgrade).place(x=450,y=440)
So, every time I execute the code, it opens a secondary window with the virtual LED. How to incorporate into my main GUI?
Thanks for your help...
The regular Turtle and Screen always create a new window.
The turtle module has another RawTurtle class which works exactly the same as Turtle, but draws on an existing tk.Canvas, turtle.TurtleScreen or turtle.Screen instance that you must provide. Take a look at the turtle docs, they have lots of examples too
You should also call mainloop() on the Tk() window at the end of your code so that the window stays open.
(It's also a good idea to store the instances of your LabelFrames and all the widgets in variables if you're likely to change any of their properties or text later on)
from tkinter import Tk, Label, LabelFrame, Button, Canvas
from turtle import TurtleScreen, RawTurtle
def fw_upgrade():
print("pas")
########## Main Window Design##########
main = Tk()
main.geometry('860x500+65+0')
main.title('Firmware Upgrade System 1.0')
label1 = Label(main,text='Firmware Upgrade System 1.0', font=("Tahoma", 20))
label1.place(x=0,y=0)
##! UUT 1 ####################################
frame1 = LabelFrame(main, text=' UUT 1 INFORMATION : ', font=("Tahoma", 10), height=120, width=420, bd=2, relief='groove' )
frame1.place(x=10,y=40)
frame2 = LabelFrame(main, text='Serial Number',font=("Tahoma", 10), height=47, width=110, bd=3, relief='ridge')
frame2.place(x=13,y=60)
frame3 = LabelFrame(main, text='Firmware Version',font=("Tahoma", 10), height=47, width=119, bd=3, relief='ridge')
frame3.place(x=126,y=60)
btn = Button(main, text="FIRMWARE UPGRADE", font=("Tahoma", 12), height=1, width=20, command=fw_upgrade)
btn.place(x=450,y=440)
canv = Canvas(main)
turtlescr = TurtleScreen(canv)
canv.place(x=0, y=100)
CURSOR_SIZE = 30
turtle = RawTurtle(turtlescr)
turtle.hideturtle()
turtle.shape('circle')
turtle.shapesize(50 / CURSOR_SIZE)
turtle.color('red', 'Yellow')
turtle.showturtle()
main.mainloop()

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()

darken the whole tkinter window

I'm programming a game sa my graduation project in school. Once the manager closes the game, I want the other users to have a pop up that the game is ended and a button to click on in order to continue.
I want the whole window (with all the widgets) to get darker (not totally black but darker). Everything besides the pop up and the "continue" button
Enyone has a clue what I can do? I searched on the Internet and found nothing :/
Not able to use ImageGrab on linux, here is my solution:
import tkinter as tk
import numpy as np
import time
import pyscreenshot as ImageGrab
from PIL import Image, ImageTk
def grab(widget):
box = (widget.winfo_rootx(), widget.winfo_rooty(), widget.winfo_rootx()+widget.winfo_width(), widget.winfo_rooty()+widget.winfo_height())
grab = ImageGrab.grab(bbox=box)
return grab
def darken(widget):
widget.overlay = ImageTk.PhotoImage(Image.fromarray(np.asanyarray(grab(widget))//2))
cvs = tk.Canvas(widget, width=widget.winfo_width(), height=widget.winfo_height())
cvs.place(x=0,y=0)
cvs.create_image(0, 0, anchor='nw', image=widget.overlay)
return cvs
root = tk.Tk()
def apply_dark():
cvs = darken(root)
b = tk.Button(root, text='Ok')
def d():
cvs.destroy()
b.destroy()
b.config(command=d)
b.place(x=50, y=10)
tk.Label(root, text='Label1').grid(row=0, column=0)
tk.Label(root, text='Label2').grid(row=0, column=1)
tk.Button(root, text='Button1').grid(row=1, column=0)
tk.Button(root, text='Darken', command=apply_dark).grid(row=1, column=1)
root.mainloop()
Fiddle around with this code. Of course this is in fact just a quick bodge (non-reliable, inefficient), but still - works for me.
Hope that's helpful!

okay so I created this code to create a GUI but i need to add buttons and when ever i try it creates a new window. what should I do?

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...

Why is tkinter not working in visual studio?

I'm working on a new project in visual studio as shown in the code below, and the GUI using Tkinter is not working in visual studio. This is my first time using visual studio and I can't seem to find why it won't work.
from tkinter import *
import tkinter as ttk
#import os #not needed
root = Tk()
#Setting up the window
root.geometry("250x100")
root.resizable(width=False, height=False)#Disables user to resize window
root.title("Login")
#Temp "DataBase"
users=[("josh","python")] #<<<here ''josh'' is user and ''python'' i5s password
admins=[("josh1","python1")]
# Login and signup function
def login(): #login function
if (t1.get(),t2.get())in users: #Temp for testing
root.destroy()
import MainWindow
# os.system("MainWindow") #does not work
print("welcome")
elif (t1.get(),t2.get())in admins: #Temp for testing
root.destroy()
import AdminMainWindow
# os.system("AdminMainWindow") #does not work
print("welcome Admin")
else:
error.config(text="Invalid username or password")
def signup(): #signup function
root.destroy
import SignupWindow
# os.system("SignupWindow") #does not work
#arranging display varables
top = Frame(root)
bottom = Frame(root)
top.pack(side=TOP, fill=BOTH, expand=True)
bottom.pack(side=BOTTOM, fill=BOTH, expand=True)
#error placement and font
error = Label(root, font=("blod",10))
error.place(x=40,y=55)
#input display setup
l1 = Label(root,text="Username:")
l2 = Label(root,text="Password:")
t1 = Entry(root, textvariable=StringVar())
t2 = Entry(root, show="*", textvariable=StringVar())
b1 = Button(root,text="Login", command=login)
b2 = Button(root,text="Signup", command=signup)
#organising
l1.pack(in_=top, side=LEFT)
t1.pack(in_=top, side=LEFT)
l2.pack(side=LEFT,)
t2.pack(side=LEFT,)
b1.pack(in_=top, side=BOTTOM)
b2.pack(in_=bottom, side=BOTTOM)
#end of Tk loop
root.mainloop()
It comes up with the python command line and says press any key to continue.
I also looked online and they all say it because people don't end the Tk loop, but I have.
before you make a new project I created a new file and places all the code in there. then add one code to VS at a time then it works but not when you do it all together.
On ms-windows, python programs using tkinter should have the extension .pyw. And this extension should be associated with pythonw.exe rather than python.exe.
Using pythonw.exe will prevent the cmd.exe window from appearing when your python script has a GUI.

Resources