how to make modern button in tkinter - python

I want to have abutton like this in tkinter (Modern window 10 buttons):
However, I get this button:
The code for my button is:
from tkinter import Tk,Button
root=Tk()
Button(root,text='OK').pack()

Another alternative to create button is to create a label and bind it to the action functions. In the below example .bind() is used to connect the label with respective function. You can design according to your requirements.
from tkinter import *
def OnPressed(event):
print('Hello')
def OnHover(event):
But.config(bg='red', fg='white')
def OnLeave(event):
But.config(bg='white', fg='black')
root = Tk()
But = Label(root, text='Hi', bg='white', relief='groove')
But.place(x=10, y=10, width=100)
But.bind('<Button-1>', OnPressed)
But.bind('<Enter>', OnHover)
But.bind('<Leave>', OnLeave)
root.mainloop()

The themed widgets are in 'themed Tk' aka ttk.
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
ok = ttk.Button(root, text='OK')
ok.pack()
root.mainloop()
Avoid from tkinter import * as both tkinter and tkinter.ttk define Button and many other widgets.
If you use this on Windows you should get something looking like a native button. But this is a theme and can be changed. On Linux or MacOS you will get a button style that is appropriate to that platform.

vol_up = Button(root, text="+",activebackground='orange',bg='yellow')
vol_up.pack(side='top')

Related

how do I fix AttributeError: 'Tk' object has no attribute 'open'

I'm trying to make a button that opens another python file
import os
import tkinter as tk
from tkinter import ttk
# root window
root = tk.Tk()
root.geometry('300x200')
root.resizable(False, False)
root.title('juststop')
os.startfile(r'C:\Users\75259\PycharmProjects\pythonProject9\main.py')
open_button = ttk.Button(
root,
text='calculater',
)
open_button.pack(
ipadx=5,
ipady=5,
expand=True
)
root.mainloop()
I have tried a multitude of things but non seem to work
The way you have your os.startfile implemented calls the function right away. One way to solve this is to create a function to use it when called and link it to your button. Below is a modification of your example that will only open the file when you press the calculater button.
import os
import tkinter as tk
from tkinter import ttk
# root window
root = tk.Tk()
root.geometry('300x200')
root.resizable(False, False)
root.title('juststop')
def open_file():
os.startfile(r'C:\Users\75259\PycharmProjects\pythonProject9\main.py')
open_button = ttk.Button(root, text='calculater', command=open_file)
open_button.pack(ipadx=5, ipady=5, expand=True)
root.mainloop()

Tkinter not displaying default Entry variable until button is added

I am trying to create a GUI in Python 3.9.1 with tkinter and am having issues getting a default value to show in an Entry box.
from tkinter import *
from tkinter import ttk
class GUI:
def __init__(self, root, *args):
self.root = root
self.initial_frame = ttk.Frame(self.root)
self.initial_frame.grid(row=0)
ttk.Label(self.initial_frame, text="User question here?").grid(row=1, column=1)
self.ext_len = StringVar(value=4)
ext_len_entry = ttk.Entry(self.initial_frame, textvariable=self.ext_len)
ext_len_entry.grid(row=1, column=2)
root = Tk()
GUI(root)
root.mainloop()
Note: official tkinter docs say to use from tkinter import * and from tkinter import ttk.
See this link and look in the Python section in the step-by-step walkthrough https://tkdocs.com/tutorial/firstexample.html
I've also tried using self.ext_len.set(4) after initializing the entry but before putting it on the grid. I've tried self.ext_len.insert(0,4) as well.
If I add a button with a callback that does nothing, the 4 shows up in the entry box. I found that I don't even have to render it on the grid. Just initializing it is enough. A button without a callback does not work.
Working code:
from tkinter import *
from tkinter import ttk
class GUI:
def __init__(self, root, *args):
self.root = root
self.initial_frame = ttk.Frame(self.root)
self.initial_frame.grid(row=0)
ttk.Label(self.initial_frame, text="User question here?").grid(row=1, column=1)
self.ext_len = StringVar(value=4)
ext_len_entry = ttk.Entry(self.initial_frame, textvariable=self.ext_len)
ext_len_entry.grid(row=1, column=2)
ttk.Button(self.initial_frame, text="Test button", command=self.test_func)
def test_func(self, *args):
pass
root = Tk()
GUI(root)
root.mainloop()
Why does initializing the button cause it to work?

Functionalities such as bg and font from Tkinter package is not working

I'm working on an Email Spam Classifier model and when I wanted to make a GUI, tkinter package is showing errors like
TclError: unknown option "-font"
TclError: unknown option "-borderwidth"
TclError: unknown option "-bg"
TclError: unknown option "-relief"
It seems like none of the functionalities of the Tkinter package seems to work. I've tried a lot of methods but still no result.
Please help me.
I've attached my code below
import tkinter as tk
from tkinter import *
from tkinter.ttk import *
from ttkthemes import ThemedTk
def GUI():
root = tk.Tk()
root.geometry('500x200')
root.maxsize(500,200)
root.minsize(500,200)
root.title('Email Spam Classifier')
root['bg'] = "white"
title = Label(root,text="Email Spam Classifier",font=('verdana',15,'bold'))
title.place(x=180,y=5)
Label(root,text="Enter mail to Classify ",font=('verdana',10,'bold')).place(x=50,y=50)
url = Entry(root,width=50)
url.place(x=50,y=80)
button = Button(root,text="Predict",font=('verdana',8,'bold'))
button.place(x=360,y=78)
root.mainloop()
GUI()
Please help me with this.
I'm new to this community, so if I had done some mistake in alignment of the text or method of asking, excuse me please.
Thanks in advance!
The problem is wildcard import.
The Button class is present in both the tkinter module and the tkinter.ttk module.
If the tkinter.ttk import follows the tkinter import, it overrides widgets with the same class names. And vice versa.
In your code, the button is tkinter.ttk.Button.
The style properties for this button are set differently.
If you want to use tkinter.Button, you need to explicitly specify which button it is.
button = tk.Button(root,text="Predict",font=('verdana',8,'bold'))
If you are using ttkthemes, you need to use ttk widgets to apply the theme to them.
import tkinter.ttk as ttk
from ttkthemes import ThemedTk
def GUI():
root = ThemedTk(theme="arc") # light theme
root.geometry('630x200')
root.maxsize(630, 200)
root.minsize(630, 200)
root.title('Email Spam Classifier')
# this will work too:
# root["background"] = "white"
# root.config(background="white")
style = ttk.Style()
# font for all ttk Buttons
style.configure("TButton", font=('verdana',8,'bold'))
# font for all ttk Labels
style.configure("TLabel", font=('verdana',10,'bold'))
# font for a specific ttk Label
style.configure("Title.TLabel", font=('verdana',15,'bold'))
title = ttk.Label(root, text="Email Spam Classifier",
style="Title.TLabel")
title.place(x=180, y=5)
enter_label = ttk.Label(root, text="Enter mail to Classify ")
enter_label.place(x=50, y=50)
url = ttk.Entry(root, width=50)
url.place(x=50, y=80)
print(url.winfo_reqwidth()) # 412
# the button has been moved because it is overlapping the entry
button = ttk.Button(root, text="Predict")
button.place(x=412+50, y=79)
print(button.winfo_reqwidth())
root.mainloop()
GUI()

how to handle the "delete window" protocol on messageboxes- Python tkinter

Let's say I have a message box in Python tkinter, for example:
from tkinter import messagebox
messagebox.showinfo("Example", "This is an example")
I want to be able to handle the message box's closing protocol. I know you can do it with tkinter windows, like so:
window.protocol("WM_DELETE_WINDOW", on_closing)
But my question is how do you do it with message boxes?
I figured out that what I wanted to do isn't possible (with the help of #Matiiss). So what I did instead was I created a toplevel widget, added buttons to it and handled its closing protocol. Example:
import tkinter as tk
from tkinter import messagebox
window = tk.Tk()
window.title("Example")
window.state("zoomed")
def protocol_handler():
# code
window2 = tk.Toplevel(window)
window2.geometry("300x220+500+200")
label1 = tk.Label(window2, text="Would you like to continue?")
label1.place(x=20, y=10)
button1 = tk.Button(window2, text="Yes")
button1.place(x=50, y=100)
button2 = tk.Button(window2, text="No")
button1.place(x=100, y=100)
window2.protocol("WM_DELETE_WINDOW", protocol_handler)
window.mainloop()

Trying to write a tkinter popup with dynamic text and confirmation button

I am trying to make a popup for messages such as "Action completed" and I want a button on it to close the popup (A simple OK which quits only the popup). It also sometimes moves behind the window which is annoying. I have some code for the button but it has an issue with the geometry of the shape since the shape isn't exactly defined as it is variable through the size of font and text length.
import tkinter as tk
from tkinter import *
import pygame
import sys
def Text(Text_input, Font, Background, Foreground):
my_window = tk.Tk()
my_window.title(Text_input)
my_window.geometry()
help_label = tk.Label(my_window, font = Font, bg=Background, fg=Foreground, text = Text_input)
help_label.grid(row=0, column=0)
button = tk.Button(my_window, text="QUIT", fg="red", command=quit)
button.pack(side=tk.BOTTOM)
my_window.mainloop()
Text("Hello", "calibri 80", "white", "black")
From my own experience wanting to achieve this, I wrote a simple tkinter code export_success.py as follows:
from tkinter import *
from tkinter import ttk
import sqlite3
from tkinter.ttk import *
root = Tk()
root.geometry('280x100')
root.title("Export Status")
root.config(bg="")
style = ttk.Style()
label_0 = Label(root, text="Export Successful", width=20, background="white", foreground="grey15", font=("Arial, bold", 15)).place(x=50, y=23)
exit1 = Button(root, text='Exit', style='C.TButton', width=11, command=root.destroy).place(x=100, y=60)
root.mainloop()
I then just insert this code os.system('python export_success.py') at the end of my tkinker window that I'm working on.
In this case I'm exporting my information and wanted to know when it is successful.
I am not saying this is best practice and there might be other ways, I just found it to work out for me.

Categories