I am using Tkinter to make GUI. I included a messagebox in my code, here's the detail:
from tkinter import *
from tkinter.messagebox import *
root=Tk()
askyesno(message="Are you sure you want to choose this text file?")
When the askyesno pops up, there are 2 buttons: Yes and No, but I want to customize these buttons to "OK" and "Cancel", for example. I have been looking for how to customize Tkinter messagebox buttons many times but I found nothing. Any help? Thanks
Use messagebox.askokcancel
from tkinter import Tk,Button,messagebox
root=Tk()
def create_message():
messagebox.askokcancel("Title",'Hello')
b=Button(root,text='Message',command=create_message)
b.pack()
root.mainloop()
You can simply use
from tkinter import *
from tkinter import messagebox
root = Tk()
button=Button("Message",)
messagebox.askokcancel()
Related
Here's the code
from tkinter import*
a= Tk()
a.title("test")
a mainloop
It doesn't give an error just says press any key to continue
The problem is the a mainloop part. It should be a.mainloop() instead.
Updated code:
from tkinter import *
a = Tk()
a.title("test")
a.mainloop()
Hope this helps!
My Code:
from tkinter import *
import tkinter as tk
root=Tk()
root.title("Program")
root.geometry('500x300')
root.mainloop()
This is my window and I want to add new button like here:
Thanks for help!
Actually, You can't add a button to the title bar in Tkinter. if you want to do that you have to build your own title bar using Tkinter widgets.
I've been working on a simple program that make a button output something. But when i run it,
this
(I got this from the internet btw) does not show up. Is somethoing wrong with the code or something?
Please help me so the window above can appear :)
Code:
from Tkinter import *
def asdf():
print('test')
tk = Tk()
b = Button(tk, text="test", command=asdf)
b.pack()
You forgot to call the Tk.mainloop method at the end of your program:
from Tkinter import *
def asdf():
print('test')
tk = Tk()
b = Button(tk, text="test", command=asdf)
b.pack()
##############
tk.mainloop()
##############
Doing so starts Tkinter's main event loop and creates the window.
It seems you are using Python3, as there are parentheses after print, so from Tkinter import * should be from tkinter import *. Python is case-sensitive. You also forgot to call root.mainloop() at the end of your code as #user2555451 mentioned, although a window should appear all the same, but stop responding when any event occurs (e.g., clicks, key presses, focus changes).
import tkinter.messagebox
a = tkinter.messagebox.askquestion('','hi')
After the 'askquestion' window closes the tkinter window still remains.
I can resolve this by the following:
import tkinter.messagebox
top = tkinter.Tk()
a = tkinter.messagebox.askquestion('','hi')
top.destroy()
This destroys the window.
My question is:
Is there a way to destroy the window without creating a reference to it?
I tried:
import tkinter.messagebox
a = tkinter.messagebox.askquestion('','hi')
tkinter.Tk().destroy()
but that has no effect.
If you destroy the root window, Tkinter try to recreate one when you call askquestion.
Don't destory the root window. Instead use withdraw.
import tkinter.messagebox
tkinter.Tk().withdraw()
a=tkinter.messagebox.askquestion('','hi')
why doesn't my python program have default windows 7/8/xp buttons but rather dull windows 2000 buttons? How can I fix this?
My dull buttoned program
What I expect :
Use the ttk module for those effects:
import Tkinter
import ttk
root = Tkinter.Tk()
ttk.Button(text="Hello").grid()
root.mainloop()
Example: