Tkinter Messagebox causing Entry to disable - python

So I was creating a simple input window with Tkinter but whenever i have a showinfo displaying i can't type in the entry box
import tkinter as tk
from tkinter import *
from tkinter.messagebox import *
root = tk.Tk()
root.title("hello world")
root.minsize(700,600)
abc = StringVar()
abc.set("abc")
Entry(root, bd = 1, width = 50, textvariable=abc).pack(side = TOP)
showinfo('info', 'hello')
root.mainloop()
I'm not sure if there is something wrong with my Python (3.4) or tkinter but whenever i take out the showinfo line I can type into the Entry box but when its there i can't.

tkinter messagebox default dialog boxes are modal. What this means is that you need
to close the child window(the tkinter messagebox) before you can return to the parent application.
So, there is nothing wrong with your python or tkinter; This behavior is intended.
Don't show the tkinter messagebox before the event loop is started. Try this:
import tkinter as tk
from tkinter import *
from tkinter.messagebox import *
def callback():
showinfo("info", "hello")
root = tk.Tk()
root.title("hello world")
root.minsize(700,600)
abc = StringVar()
abc.set("abc")
Entry(root, bd=1, width=50, textvariable=abc).pack(side=TOP)
Button(root, text="OK", command=callback).pack()
root.mainloop()

The solution i made for this is overriding the messagebox.showerror so for example i made
import logging
from tkinter import messagebox
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s [%(levelname)s] %(name)s "%(message)s"',
)
LOGGER = logging.getLogger(__name__)
def test_showerror(title, message):
LOGGER.debug(f'{title} Message -> {message}')
messagebox.showerror = test_showerror
actually that's how i deal with many problems that face me during writing tests.. i override utility functions to add logging or avoid a case.

Related

Python tkinter button showing script to scrolledtext box

When i press the start button, it runs the other script but only shows it in the other terminal, not in the GUI scrolledtext box i have. is there a simple fix for my problem? not finding an answer anywhere.
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
import sys
import os
import tkinter.scrolledtext as tkst
root = Tk()
root.title("WLP")
root.geometry("950x450")
root.iconbitmap("Vo1d.ico")
root.configure(bg='#0c0c0c')
def clickstart():
os.system('python WLP.py')
Button1 = Button(root, text="START", bg="#0c0c0c", fg="#C0C0C0", command=clickstart) #)
Textbox = tkst.ScrolledText(root, width=75, height=10, bg="#0c0c0c", fg="#C0C0C0")
Button1.grid(row=10, column=5, pady=15)
Textbox.grid(row=17, column=5)
root.mainloop()
This is far from a good way to do it, but this should work (after importing subprocess)
def clickstart():
output = subprocess.run(['python', 'WPL.py'], stdout=subprocess.PIPE, check=True)
output = str(output.stdout, encoding='utf-8')
Textbox.insert(1.0, output)

How to call another python script from a .py file in a new window

I ran into a problem where I want to click a button on a fullscreen app.
test1
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import os
root = Tk()
root.title('Gamesim')
root.geometry('500x400')
def cmdopen():
os.system('C:\Users\User\Desktop\test2.py')
btn = Button(text='test', command=cmdopen)
btn.pack()
root.mainloop()
test2
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import os
root = Tk()
root.title('Gamesim')
root.geometry('1870x1080')
root.attributes("-topmost", True)
btn = Button(text='test2')
btn.pack()
root.mainloop()
What it does it displays the test2 interface, but test one stops responding. What I want is that the test2 will apear above and both will respond and are diffrent windows.
Im bad in english so sorry if I have some problems.
If you're okay with having one "master" window that keeps track of the other windows, then you can do something like this:
from tkinter import *
from tkinter.ttk import *
from functools import partial
class subWindow(Toplevel):
def __init__(self, master=None):
super().__init__(master=master)
def createSubwindow(master):
"""Creates a subWindow of 'master' and sets it's options"""
subWin = subWindow(master)
subWin.title('SubWindow')
subWin.geometry('500x400')
subWin.attributes("-topmost", True)
btn = Button(subWin, text='Button Inside of SubWindow')
btn.pack()
# Creating the master-window
root = Tk()
root.title('MasterWindow')
root.geometry('500x400')
# Creates a partial of the createSubwindow, so that we can execute it easier in the button.
subWinPartial = partial(createSubwindow, root)
# Installs the button, with the partial function as a command.
btn = Button(root, text='Create Sub Window', command=subWinPartial)
btn.pack()
# Runs the mainloop, that handles all the windows.
root.mainloop()

Change right click event of tkinter command

I want to detect the right click event on tkinter Menu command.
Consider code below.
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
menu_button = ttk.Menubutton(root, text="MENU")
menu_button.grid()
m = tk.Menu(menu_button, tearoff=False, activeborderwidth=0)
menu_button["menu"] = m # To avoid garbage collection
m.add_command(label="an option", command=lambda: print("option1"))
m.add_command(label="another option", command=lambda: print("option2"))
root.mainloop()
When I click an option or another option, the commands are called as expected. But want I want to do is catch right click event. Can anyone knows that how can I detect it?
use button.bind("<Button-3>", event). Consider this code:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
button = tk.Button(root, text='right click this')
button.pack()
button.bind("<Button-3>", lambda e: print('You right clicked'))
root.mainloop()

How to make python tkinter match button to OS

Why does tkinter.Button() appear as some ancient OS style button, while a messagebox like tkinter.messagebox.showinfo() comes with an OK button using the current version of the OS?
My OS is Windows. Not sure if this problem exists on Mac OS, but either way the people using my tools are on Windows.
An example snippet of code I found here shows how the buttons are different.
Image:
Question:
Is there a way to make tkinter.Button() look like the button inside a messagebox, which seems to be using the current OS style?
Code:
from tkinter import *
from tkinter import messagebox
window = Tk()
window.title("Welcome to LikeGeeks app")
window.geometry('350x200')
def clicked():
messagebox.showinfo('Message title', 'Message content')
btn = Button(window,text='Click here', command=clicked)
btn.grid(column=0,row=0)
window.mainloop()
Use tkinter.ttk to get themed version
from tkinter.ttk import *
from tkinter import messagebox
window = Tk()
window.title("Welcome to LikeGeeks app")
window.geometry('350x200')
def clicked():
messagebox.showinfo('Message title', 'Message content')
btn = Button(window,text='Click here', command=clicked)
btn.grid(column=0,row=0)
window.mainloop()
doc
You can use tkinter.ttk which provides a modern OS style theme to your TK widgets. Example from the Python tkinter docs:
from tkinter import ttk
import tkinter
root = tkinter.Tk()
ttk.Style().configure("TButton", padding=6, relief="flat",
background="#ccc")
btn = ttk.Button(text="Sample")
btn.pack()
root.mainloop()
#Output:

How to create a multiline entry with tkinter?

Entry widgets seem only to deal with single line text. I need a multiline entry field to type in email messages.
Anyone has any idea how to do that?
You could use the Text widget:
from tkinter import *
root = Tk()
text = Text(root)
text.pack()
root.mainloop()
Or with scrolling bars using ScrolledText:
from tkinter import *
from tkinter.scrolledtext import ScrolledText
root = Tk()
ScrolledText(root).pack()
root.mainloop()
Just use Text() widget.
For example:
import tkinter as tk
root = tk.Tk()
text = tk.Text(root)
text.pack()
root.mainloop()
Output:

Categories

Resources