I'm working on a basic PIN interface for a touch screen with Python 2.7 Tkinter and ttk. I'm developing the script on Windows but it will eventually be loaded on a Linux OS.
I am trying to prevent what is shown on the "6" button of the picture bellow, i.e. a dashed border around the button lastly clicked. Since I don't want people to easily steal the PIN from my users, I have to prevent this from happening, otherwise it becomes really easy to find out what their PIN are just by looking at the screen. I have noticed that this behavior becomes even more obvious on LINUX with something like a thick white border around the button.
I am calling my buttons inside a loop like this:
ttk.Style().configure('TButton', padding=11, relief="flat", background="#ccc", foreground="#393939", width=4,font='Arial 9')
btn = ttk.Button(window, text = txt, command = lambda txt=txt:self.addChar(txt))
btn.grid(row=row, column=col, padx=1, pady=1)
The solution is pretty simple: modify your addChar function to move the focus back to some other widget after it inserts the character.
Related
Here's an excerpt for a main menu using Python Tkinter
master = Tk()
lbl = Label(master, text = "Main Menu:")
lbl.grid(row=0,column=1)
def list():
import list
listb = Button(master, text = "Patient List", command=list, width=10)
listb.grid(row=1, column=1)
There's a button "Patient List" that when pressed, opens a window specified from a different python file.
My problem is that the Main Menu window suffers a bug where the window will collapse. The new window comes out fine, but after closing it, I can't use my Main Menu.
Also, on my other buttons. The Main Menu doesn't collapse, but let's say I have a button Add Patient and after clicking it, the Add Patient window appears. It's functional. And then I close it, either using the quit button I programmed in it, or the close button. The Main Menu won't open the Add Patient window again.
So how can I smoothly navigate from one python program to another? Without the main menu collapsing. While being able to open a window over and over again.
My Patient List program has a lot of customized gui programming in it. I think it's because I use a grid in my Main Menu, and it's not a grid in my Patient List. But it shouldn't be affecting the Main Menu because they're supposed to be separate programs.
When doing such program with TKinter I generally use Screen classes.
To your example it would be for example:
"MainScreen", "AddPatienScreen", ...
All are subclass of the Frame tkinter class.
My Tk app only display one screen and furnish function to destroy and replace one screen by another one.
I have not access to any code now, but I invite yourself to try this approach if you understand it. It lead to well separated code and had good results.
I may provide you a very short example if required.
I am using Tkinter for my python script. At current stage, gui looks ugly because of too many options/widgets in one screen. Not all of them are needed all the time and it can be divided into four parts. Hence my idea is to have them expand the frame (one frame for each part) with its options/widgets in the current window.
Is it possible in tkinter to expand the window/frame when a button or a checkbox is clicked?
ps - I have tried opening each part in new window but this makes gui unnecessarily complicated.
Thanks!
So, your gonna need to define what happens when the button is clicked-
def buttonClicked():
root.geometry("350x300")
where root is the window and then on the button you will need to write -
btn = tkinter.Button(text = "Button", command=buttonClicked)
btn.pack
Hope that answers your question!
Is there a bug in the natural scrollbar trough click event handler, is something wrong with the way I set it up, or something else? I ask because it doesn't always work (when it doesn't work, it either does nothing or else moves to the wrong position). The scrollbars otherwise work fine. Sometimes the trough clicks do work, but a lot of the time they don't. (More than one or two clicks is often needed to discover the problem.)
hbar=Scrollbar(newTabFrame, orient=HORIZONTAL, bg=self.d["hbgcolor"], troughcolor=self.d["htcolor"]);
vbar=Scrollbar(newTabFrame, orient=VERTICAL, bg=self.d["vbgcolor"], troughcolor=self.d["vtcolor"]);
scroll=Text(self, newTabFrame, font=self.d["font"], undo=True, tabs=("0.4c"), wrap=self.d["wrap"], xscrollcommand=hbar.set, yscrollcommand=vbar.set, bg=self.d["bgcolor"], fg=self.d["fgcolor"], insertbackground=self.d["insertcolor"]);
vbar.grid(row=1, column=2, columnspan=2, rowspan=4, sticky=N+S);
vbar.config(command=scroll.yview);
hbar.grid(sticky=E+W);
hbar.config(command=scroll.xview);
Once again, it's only when I click on the troughs where I experience a problem. All the other scrollbar functionality (which is most of it) works fine. For those who don't know, a scrollbar trough is the long part of the scrollbar that does not move.
My scrollbars are attached to Text widgets inside Frames that are inside of ttk.Notebook widget tabs.
To directly answer your question, no, there is no known bug in the scrollbar code. This code has been in use for more than a decade without anyone reporting issues with the trough. That doesn't mean a bug isn't possible, but it is highly unlikely. There also doesn't appear to be any problems with how you defined the scrollbar or how you attached it to the text widget. .
Based on comments to the original question, it sounds like you have some bindings in your app that may be interfering with the default behavior of the scrollbar.
for my board game, I need to press a certain button and after that, disable it (to prevent the user or the computer to place a "peace" in the same place). The problem I have with this is basically design-wise: as you would know, when a button is disabled in tkinter, it turns grayed-out, kinda "visually blocking" the piece I placed... Here is an image of what I'm talking about:
My question is, how would I disable a button, but keep it as if it wasn't disabled? I searched and the only answer I came about was basically getting rid of the button and replacing it with an image... If that's my best bet, how would I do this? the grid is created with a for loop that fills a list with buttons, and then displays them into a frame using the grid() method. This would look like:
Is there another way to achieve this with tkinter methods? I would rather not change the buttons to an image with a bound event, that seems too complicated given that I'd have to mess with my loop. Thanks!!
Thanks!
You can control the foreground and background colors with a variety of options, such as foreground, background, disabledforeground, and highlightbackground. Have you tried setting any of those to get the visual behavior you want?
You also have the option of disabling the callback, so the button will press but it won't do anything. To disable it, simply configure the command option to be None:
the_button.configure(command=None)
Turns out in order to disable the button but keeping its appearance, you have to disable its callback to make it "unresponsive" (as stated by #Bryan Oakley and #martineau, thanks!) and also change its 'relief' as to make it stay "fixed" (it kind of shifts into the bottom right corner, but that's good enough).
Imagine you have this generic button:
button = tkinter.Button(frame, height=0, bg='blue',\
activebackground='blue',width = 0, text = " ", image = self.blank, \
command=lambda row=row_index, \
column=column_index: \
self.button_clicked(row,column))
Now when you want to disable it but keeping its original appearance, you change the following attributes of the button:
button['command'] = 0 #this disables the callback
button['relief'] = 'sunken' #makes the button fixed
As easy as that! thanks!
I am trying to remove the border that is coming with the buttons in my program.
I've tried adding bd=0 and highlightthickness=0 to the Button() but it's just not working.
Can anyone suggest how to do this?
Current code example:
self.highscore_button_image = ImageTk.PhotoImage(file="images/buttons/highscore.png")
highscore_button = Button(self.menu_window, bd=0, highlightthickness=0, image=self.highscore_button_image)
highscore_button.place(x=266, y=273)
Buttons: I want to remove the white border... It also comes with a regular button without an image
Python version: 2.7.1
OS: Mac OSX Lion
Using Tkinter at the moment...
I'm not sure you can get rid of that border for a button. You could use a Label widget instead of a Button widget, and bind mouse events to it if it needs to be clickable. The Label widget doesn't have that button-y outline.
highscore_button = Button(....,relief=FLAT)
The relief style of a widget refers to certain simulated 3-D effects around the outside of the widget, it can be RAISED, SUNKEN,FLAT,GROOVE,RIDGE.
Removing border on buttons on Mac is currently not allowed. OS X is quite strict at this, there is no way to remove the border if using tkinter. I recently just had the same problem and all the methods I've tried are not working properly on Mac while most of them work on Windows.
However, it is possible to do it on Windows using "relief=FLAT" or "highlightthickness=0". You can also use "padx=0, pady=0" to have a really thin border, it will still exist.
You can also check out this site which talks about styling tkinter widgets(not only buttons but all the widgets): http://effbot.org/tkinterbook/tkinter-widget-styling.htm
You may change the colour of the border(ButtonFace) to the same colour of your background according to this site.
There is a similar question on button styling here as well: How to change the foreground or background colour of a Tkinter Button on Mac OS X?