How to make python tkinter match button to OS - python

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:

Related

Tkinter window not opening in pycharm

I am eriting code in pycharm with tkinter but the window is not opening. May someone assist?
`
import tkinter
window = tkinter.Tk()
button = tkinter.Button(window, text="Do not press this button! >:-(", width=40)
button.pack(padx=10, pady=10)
`
i tried checking my script for bugs but nothing
This has nothing to do with Pycharm, but with tkinter library and how to use it.
You are missing 2 important stuff:
Button is in ttk.py file inside tkinter library: from tkinter import ttk
Execute the whole script with mainloop
Try this:
import tkinter
from tkinter import ttk # Import ttk file from tkinter library
window = tkinter.Tk()
window.title("Coolest title ever written")
button = ttk.Button(window, text="Do not press this button! >:-(", width=40) # Use Button from the import ttk file
button.pack(padx=10, pady=10)
window.mainloop() # Execute the whole script

Is there away to hide the text inside a button/label in Tkinter?

I'm a Python beginner that trying to learn my way through Tkinter, and I need your help.
Let say I create a simple button like this:
import tkinter as tk
window = tk.Tk()
button = tk.Button(text="Hello World!")
button.pack()
window.mainloop()
Is there a way that I can hide and then display the text again? Given that I can create two more buttons that will do the job of hiding and displaying. I have tried to use button.pack_forget(), but it will hide the entire button instead.
Any help would be appreciated.
To make it look like the buttons text vanishes,you can make the text color as same as the background color via cget method.
import tkinter as tk
def hide_text():
color = button['bg']
button.config(foreground=color, activeforeground=color)
window = tk.Tk()
button = tk.Button(text="Hello World!",command=hide_text)
button.pack()
window.mainloop()
Approach 2 ttk.Button:
import tkinter as tk
from tkinter import ttk
def hide_text():
button.config(text='')
window = tk.Tk()
button = ttk.Button(text="Hello World!",width=100,command=hide_text)
button.pack()
window.mainloop()
Approach3 using style:
import tkinter as tk
from tkinter import ttk
def change_button_style(event):
widget = event.widget
if widget['style'] == 'TButton':
widget.configure(style='VanishedText.TButton')
else:
event.widget.config(style='TButton')
BACKGROUND = '#f0f0f0'
FOREGROUND = '#000000'
window = tk.Tk()
window.configure(bg=BACKGROUND)
style = ttk.Style()
style.theme_use('default')
button = ttk.Button(text="Hello World!",style='VanishedText.TButton')
button.bind('<ButtonPress-1>',change_button_style)#,command=change_button_style
button.pack()
style.map('VanishedText.TButton',
foreground =[('disabled',BACKGROUND),
('!disabled',BACKGROUND),
('pressed',BACKGROUND),
('!pressed',BACKGROUND),
('active',BACKGROUND),
('!active',BACKGROUND)],
background =[('disabled',BACKGROUND),
('!disabled',BACKGROUND),
('pressed',BACKGROUND),
('!pressed',BACKGROUND),
('active',BACKGROUND),
('!active',BACKGROUND)],
focuscolor=[('disabled',BACKGROUND),
('!disabled',BACKGROUND),
('pressed',BACKGROUND),
('!pressed',BACKGROUND),
('active',BACKGROUND),
('!active',BACKGROUND)])
window.mainloop()
You Can Simply Config And Set Text :
import tkinter as tk
from tkinter import ttk
win=tk.Tk()
def hidetext():
button.config(text="")
button=ttk.Button(win,text="FooBar",command=hidetext)
button.pack()
win.mainloop()

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

How to unbold the font used to show messages in the tkinter.messagebox.showinfo() widget? [duplicate]

I'm using the simple message-boxes provided in tkinter and I'm wondering if there's any way to change the font.
This is the general idea of what I want, but the font= option doesn't work.
from tkinter import Tk
import tkinter.messagebox as tkmsg
_root = Tk()
_root.withdraw()
tkmsg.showinfo(
"Info",
"Some monospaced text",
font=("Monospace", 15)
)
_root.destroy()
Is there any way to change the font or do I have to use a custom dialog?
See here for how to change the dialog box text: Control Font in tkMessageBox
In short (copied verbatim from the link above):
You can configure the font for just dialog boxes by doing the following:
from Tkinter import *
import tkMessageBox
r = Tk()
r.option_add('*Dialog.msg.font', 'Helvetica 12')
tkMessageBox.showinfo(message='Hello')
Be sure to call r.option_clear() to set the font back to normal afterwards.
You can change the default font for captions:
import tkinter as tk
from tkinter import messagebox as mb
from tkinter import font
root = tk.Tk()
font1 = font.Font(name='TkCaptionFont', exists=True)
font1.config(family='courier new', size=20)
mb.showinfo(message='Hello')
You should write your own messagbox. Tkinter invoke system dialog for Windows or Mac and genetate dialogs for Linux. In all cases is imposible change Tkinter dialogs.
you can't.
write your own messagebox using the toplevel widgted (tkinter.Toplevel()) and label!
Something like this (from http://effbot.org/tkinterbook/label.htm)
from Tkinter import *
master = Tk()
w = Label(master, text="Hello, world!")
w.pack()
mainloop()
i hope it helps!
Edit: This is a very old answer, 3 years later someone said it's possible:
Control Font in tkMessageBox

How to set font of a messagebox with Python tkinter?

I'm using the simple message-boxes provided in tkinter and I'm wondering if there's any way to change the font.
This is the general idea of what I want, but the font= option doesn't work.
from tkinter import Tk
import tkinter.messagebox as tkmsg
_root = Tk()
_root.withdraw()
tkmsg.showinfo(
"Info",
"Some monospaced text",
font=("Monospace", 15)
)
_root.destroy()
Is there any way to change the font or do I have to use a custom dialog?
See here for how to change the dialog box text: Control Font in tkMessageBox
In short (copied verbatim from the link above):
You can configure the font for just dialog boxes by doing the following:
from Tkinter import *
import tkMessageBox
r = Tk()
r.option_add('*Dialog.msg.font', 'Helvetica 12')
tkMessageBox.showinfo(message='Hello')
Be sure to call r.option_clear() to set the font back to normal afterwards.
You can change the default font for captions:
import tkinter as tk
from tkinter import messagebox as mb
from tkinter import font
root = tk.Tk()
font1 = font.Font(name='TkCaptionFont', exists=True)
font1.config(family='courier new', size=20)
mb.showinfo(message='Hello')
You should write your own messagbox. Tkinter invoke system dialog for Windows or Mac and genetate dialogs for Linux. In all cases is imposible change Tkinter dialogs.
you can't.
write your own messagebox using the toplevel widgted (tkinter.Toplevel()) and label!
Something like this (from http://effbot.org/tkinterbook/label.htm)
from Tkinter import *
master = Tk()
w = Label(master, text="Hello, world!")
w.pack()
mainloop()
i hope it helps!
Edit: This is a very old answer, 3 years later someone said it's possible:
Control Font in tkMessageBox

Categories

Resources