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.
Related
I need help understanding the interaction between a Python script and tkinter. I am fairly sure that this question will already have been answered but I can't find it so I expect my search terms are wrong.
I have developed a Python script that I use daily that prints information as text and takes text input. There is one place where I need to display a lot of information in tabular form and I'm trying to use tkinter for that job. So I would like a window / dialog to appear to show the information and once an OK button is pressed for it to disappear again.
All the examples of using tkinter seem to enter the tkinter mainloop at the end of the script which I think means all data in and out is through tkinter.
The best I've achieved so far opens a window with all the data well presented and the button ends the script completely.
exitBut = tkinter.Button(frameButtons, text="Exit", command=exitFn)
exitBut.grid(row=0, column=0)
describeDialog.mainloop()
Can you help me understand how to create and destroy a temporary tkinter window in the middle of a Python script please.
a simple example here, maybe not best practice but shows that you can write your script as you normally would without using tkinter, show it with tkinter and then carry on without it.
import tkinter as tk
print("pretending to do something here")
root=tk.Tk()
root.title("my window")
tk.Label(root, text="show something here").pack()
tk.Button(root, text="ok", command=root.destroy).pack()
root.mainloop()
print("now we can do something else")
print("note how execution of script stops at the call to mainloop")
print("but resumes afterwards")
I'm just getting started with tkinter, so the answer to this question may be rather obvious to those with more experience... My Python program defines a root window in the main code, which includes several label widgets:
for i in range(0,20):
line.append(ttk.Label(mainframe, text=""))
line[i].grid(column=1, row=i, sticky=W)
It also has a few menu items, one of which launches a graphical subprogram that operates in its own (pygame) window. I would like to modify the labels in the main window based on events in the subprogram, which I try using:
line[i].configure(text=s)
Problem is, these new label values don't show up until the subprogram completes and control returns to the main window. Is there a way around this?
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.
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!
I have multiple buttons in my tkinter 8.5 GUI (on Windows 7). I want whatever button is focused on (tabbed over) to be selected when the user hits Enter. I know I have to bind '<Return>', but I need the rest of the gaps filled in.
Thanks in advance!
Assuming you want this to be universal to all applications in the root window you could do something similar to this.
def clickButton():
widget = root.focus_get()
if widget != root:
widget.invoke()
root = Tkinter.Tk()
root.bind("<Return>", clickButton)
root.mainloop()
That will run any command associated with the currently tabbed selection. If you want to limit it to certain buttons you can do type-checking inside of the method. Widget will be whatever widget is currently in focus via the tabbed selection. Also beware of a user hitting enter on certain widgets that may not support the invoke method.