I'm using windows xp. I want to change menubar and labels foreground and background in TKinter. But, I'm unable to change. Can I change it in windows xp or I have to upgrade it to windows 7.
from Tkinter import *
root = Tk()
menubar = Menu(root)
menubar.add_command(label = 'Label1', command = log, background = 'Black', foreground = 'Red')
root.config(menu=menubar)
root.mainloop()
I'm able to display what I want and my code is working perfectly in Linux. But, it's not changing the color in window. Do I need to use any additional commands to make it work?
There is nothing you can do. Tkinter uses a native menu object for the menus, which means they will have exactly the same look and feel of other windows menus.
from Tkinter import *
def log():
print 'in log fun'
root = Tk()
menubar = Menu(root)
menubar.add_command(label = 'Label1', command = log)
root.config(bg='red',menu=menubar)
root.mainloop()
you can config the background color, not possible to menu background color.
enter image description here
Related
I wrote this code on Pycharm (macOS) but the menu bar does not show up. Can anyone tell me why?
from tkinter import *
root = Tk()
def hello():
print("hello!")
# create a toplevel menu
menubar = Menu(root)
menubar.add_command(label="Hello!", command=hello)
menubar.add_command(label="Quit!", command=root.destroy)
# display the menu
root.config(menu=menubar)
root.mainloop()
You cannot put commands at the very top of a menubar in OSX. Also, the menu will not appear at the top of the window, it appears at the top of the screen just like with native OSX apps.
Other than the fact that your menubar has no dropdown menus, it's working as designed. Add a dropdown menu, and that menu will appear on the menubar.
How can I change the color of the menu in Tkinter? That is, I need to change the color of the menu, which is attached to the window through root.config (menu = menu). The usual use of menu.config (bg = 'black') does not help.
Code
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
menu = tk.Menu(root)
#menu.config(background='black')
root.config(menu = menu)
root.config(bg='black')
file = tk.Menu(root, tearoff=0)
file.add_command(label='GYG')
menu.add_cascade(label='Hello', menu=file)
root.mainloop()
PS Sorry if I have awkward English (I speak Russian, but the Russian version of the site did not help me)
Reply to comment
I commented out a line to show how I tried to change the background (from the screenshot this is clearly visible)
This depends on your platform. You can't change the color on OSX, and I'm pretty sure you can't change it on Windows, either. When you associate a menu with the window using the menu option of the window, tk has very little control over the menu. The menus are largely managed by the underlying OS.
The way to do it is how you're doing it. If it's not working, then it is a restriction on the platform you're working on.
I am making a calculator using tkinter and I wish to do the following:
Disable keyboard input for the Entry widget so that the user can only input through the buttons.
Even after disabling keyboard input for the Entry widget, I wish to be able to change the background and foreground of the widget.
I wish to hide the console window because it is totally useless in the use of this calculator.
I don't want to let the user resize the root window. How do I disallow the resizing of the root window?
Here is my code so far...
from tkinter import *
root = Tk()
root.title("Calculator")
root.config(background="black")
operator = ""
textVar = StringVar()
def valInput(number):
global operator
operator+=str(number)
textVar.set(operator)
display = Entry(root, textvariable=textVar, font=("Arial", 14, "bold"), bg="lightblue", fg="black", justify="right")
display.grid(row=0, column=0, columnspan=4)
btn7 = Button(root, font=("Arial", 12, "bold"), bg="orange", fg="red", text="7", command= lambda : valInput(7))
btn7.grid(row=1, column=0)
"""
And more buttons...
"""
root.mainloop()
As you can see, I can input into the Entry widget using buttons but later on, after the calculator is complete, if the user inputs characters like abcd... it will cause problems and show errors. How do I disallow keyboard entry so that I can avoid these errors?
I want to make my calculator a bit colorful. I changed the color of the root window, the buttons and also the color of the Entry widget. Is there any way to change the color of the widget even after it is disabled?
I don't need the console window while using this calculator. How do I hide it?
If I resize the root window, the calculator becomes ugly, besides, resizing the window isn't necessary. So how do I prevent the user from resizing the window?
To be able to disable keyboard input in Entry(args)
Set the state to disabled:
display = Entry(root, state=DISABLED)
To be able to disable the feature of resizing the tkinter window (so that you can't drag and stretch it.
root.resizable(0,0)
To be able to make the command prompt window disappear. (I just want the tkinter window.
Rename the file with a .pyw extension (assuming you are using windows)
Don't use from tkinter import * it's really not recommended because it pollutes the main namespace with every public name in the module. At best this makes code less explicit, at worst, it can (and it will) cause name collisions.
Have the right reflexes, use import tkinter or import tkinter as tk instead
this should work, you have to use the disabledbackground option :
import tkinter as tk
root = tk.Tk()
display = tk.Entry(root,font=('Arial', 20, 'bold'), disabledbackground='lightblue', state='disabled')
display.pack()
root.resizable(0,0)
root.mainloop()
I am just learning python, and I am trying to make a window go full screen, which I have achieved, but I am now wanting to get rid of the title bar across the top. It currently looks like the image below, but I want it to also go over the Mac top toolbar at top (like a splash screen).
from tkinter import *
root = Tk()
root.attributes('-fullscreen', True)
root.attributes('-topmost', True)
root.overrideredirect(True)
def quitApp():
# mlabel = Label (root, text = 'Close').pack()
root.destroy()
# placing the button on my window
button = Button(text = 'QUIT', command = quitApp).pack()
I believe what you want to do is use
root.wm_attributes('-fullscreen','true')
Try this instead. It should do the trick.
from tkinter import *
root = Tk()
root.wm_attributes('-fullscreen','true')
def quitApp():
root.destroy()
button = Button(text = 'QUIT', command = quitApp).pack()
root.mainloop()
If this does not work because of the MacOS then take a look at this link This useful page has sever examples of how to manage mack windows in tkinter. And I believe what you may need to get borderless fullscreen.
This bit of code might be what you need:
root.tk.call("::tk::unsupported::MacWindowStyle", "style", root._w, "plain", "none")
Note: If you do use this option then you will need to remove root.wm_attributes('-fullscreen','true') from your code or just comment it out.
Update:
There is also another bit of code for tkinter 8.5+.
If you are using python with tkinter 8.5 or newer:
root.wm_attributes('-fullscreen', 1)
Say I have some simple code, like this:
from Tkinter import *
root = Tk()
app = Toplevel(root)
app.mainloop()
This opens two windows: the Toplevel(root) window and the Tk() window.
Is it possible to avoid the Tk() window (root) from opening? If so, how? I only want the toplevel. I want this to happen because I am making a program that will have multiple windows opening, which are all Toplevel's of the root.
Thanks!
The withdraw() method removes the window from the screen.
The iconify() method minimizes the window, or turns it into an icon.
The deiconify() method will redraw the window, and/or activate it.
If you choose withdraw(), make sure you've considered a new way to exit the program before testing.
e.g.
from Tkinter import * # tkinter in Python 3
root = Tk()
root.withdraw()
top = Toplevel(root)
top.protocol("WM_DELETE_WINDOW", root.destroy)
but = Button(top, text='deiconify')
but['command'] = root.deiconify
but.pack()
root.mainloop()
The protocol() method can be used to register a function that will be called when the
Toplevel window's close button is pressed. In this case we can use destroy() to exit.