I am trying to make a bind_class to a ttk Entry widget but it does not work. It only work properly when I bind each Entry widget separately.
Would you help me?
Thank you in advance
from tkinter import ttk
from tkinter.ttk import*
from tkinter import *
root = Tk()
def clicker(event):
myLabel = ttk.Label( root, text = 'You clicked a button' )
myLabel.pack()
myEntry = ttk.Entry( root )
myEntry.pack()
root.bind_class( 'Entry', '<Button-1>', clicker )
root.mainloop()
When I replaced 'Entry' by 'TEntry' it worked perfectly
Related
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()
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()
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:
Something like this:
from Tkinter import *
root = Tk()
but = Button(root, text = "button")
but.pack()
#When I try:
but.destroy()
but.pack()
I get an error:
TclError: bad window path name ".37111768"
The pack_forget method will hide the widget and you can pack or grid it again later.
http://effbot.org/tkinterbook/pack.htm
I have managed to get it working :) here is my work:
from Tkinter import *
def changebutton():
but.destroy()
secondbut=Button(root,text="changed")
secondbut.pack()
if __name__=='__main__':
root=Tk()
global but
but= Button(root,text="button",command=changebutton)
but.pack()
root.mainloop()
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: