Is there different ways to create a menu bar? - python

I've come across two different ways to create a menu bar:
import tkinter as tk
window = tk.Tk()
menu_bar = tk.Menu() ■
window.config(menu = menu_bar) #2
window.mainloop()
import tkinter as tk
from tkinter import Menu
window = tk.Tk()
menu_bar = Menu(window) ■
window.configure(menu = menu_bar)
window.mainloop()
Question: what's the difference between these lines of code? By this I mean, why is the syntax different If they do the same? (I've marked the referred lines of code as ■). How importing Menu from tkinter affect the lines of code?

What's the difference between these lines of code?
It does the same thing(except the imports. With from tkinter import Menu, you are specifically just importing Menu and nothing else from tkinter. But in the first example, you are importing whole tkinter and you can refer to tkinter.Menu as tk.Menu. But in the second example, you just have to say Menu.
Note that in the second example you can still use tk.Menu as well as Menu. So the second import is rendered useless, and can be removed. It is better to follow the first example.
As mentioned by AST, if you say Menu(), an existing instance of Tk() will be passed as the master argument implicitly. But if you say Menu(win), you are passing win explicitly. It is always recommended to pass the parent argument explicitly while you work with multiple windows so as to not cause confusions.

Related

How do I execute a code within a tk.Toplevel()

I'm somewhat new to python (started in Nov.) and after complete my first "program" I'm trying to built the GUI using Tkinter. I want to put the program on a Toplevel that I've created and have it run, but all Tkinter tutorials only talk about widgets and I don't know how to specify that a code should run on a specific Toplevel window. The best I can figure is to run the in the section where I define the Toplevel as shown in the example below, but that is not working.
from tkinter import *
import tkinter as tk
root=Tk()
root.geometry("500x200")
root.title('Test')
Label(root, text="Test").pack()
def test():
gen_win = Toplevel(root)
gen_win.title("Test")
gen_win.geometry("500x500")
Label(gen_win, text="Test").pack()
print(2+2)
btn_test=tk.Button(root, text="test", command=test).pack(fill=tk.X)
root.mainloop()
The example program (print(2+2)) doesn't print on the toplevel. Any ideas?
#jasonharper gave the correct answer:
"Code doesn't "run on a specific Toplevel window". It just runs, and if it happens to create a widget, or modify the contents of an existing widget, that change becomes visible as soon as your code returns to the mainloop. Label(gen_win, text=str(2+2)).pack() would be the simplest way to make your addition results visible in the window."

Python, Tkinter - some general questions about the code

I have some general questions regarding working code below:
tkinter is library for graphic interface as I understand I can use it interchangeably with for example Kivy?
Would it be better to learn Kivy instead or other?
Lines import tkinter as tk and from tkinter import * do exactly the same, in the first one I have alias though?
In the code below, why do I have to use ttk in ttk.Progressbar?
I imported whole library with import tkinter as tk so why do i have to reimport ttk just for progress bar? (otherwise it is not working). I would expect to work sth. like tk.Progressbar
In the line btnProg = tk.Button(self.root, text = 'update', command=self.fnUpdateProgress), why method "fnUpdateProgress" can't have any variables? Whenever I add any, the button stop working? -> for example btnProg = tk.Button(self.root, text = 'update', command=self.fnUpdateProgress(24)) (ofc then some changes in def of the method itself)
I created progress bar (pb) as attribute of the class Test, but wolud it be better to define it as regular variable (without self)? To be honest, code works exactly the same.
Code:
import tkinter as tk
from tkinter import *
from tkinter import ttk
from CreateProgramMain import main
import GlobalVariables
class Test():
####################################################################################
def __init__(self):
self.Progress=0
self.root = tk.Tk()
self.root.title(GlobalVariables.strAppName)
self.root.geometry('400x200')
lbl = Label(self.root, text="Please choose environment.",font=("Arial Bold", 12))
lbl.grid(column=2, row=0,sticky='e')
def btnTestClicked():
main("TEST",self)
btnTest=tk.Button(self.root, text="Test Environment", command=btnTestClicked)
btnTest.grid(column=2, row=15)
#Place progress bar
pb = ttk.Progressbar(self.root,orient='horizontal',mode='determinate',length=200)
pb.grid(column=1, row=65, columnspan=2, padx=10, pady=20)
pb["value"]=self.Progress
pb["maximum"]=100
btnProg = tk.Button(self.root, text = 'update', command=self.fnUpdateProgress)
btnProg.grid(column=2, row=75)
self.root.mainloop()
def fnUpdateProgress(self): #why i cant insert variable inside?
pb["value"]=self.Progress
self.Progress=self.Progress+5
pb.update()
app = Test()
Thank you
it is upto you. However, tkinter and kivy both have their own syntaxes, commands, and their own usages. It might be a little difficult to convert tkinter code to kivy.
it is upto you
Yes. In the first, you have imported tkinter as tk. In the second one. You have done a wild card import. You have imported everything
Tkinter is a folder containing various modules. It contains a file called ttk.py which you have to import to access ttk.
All other classes like Label, Entry, Tk is present in __init__.py
you have to use lambda for it. If you call the function, it will be executed wlright away and the returned value is kept as the command.
Doing command=self.fnUpdateProgress(24)) will execute the function right away. Then, the returned value is kept as a command. Here, it returns None. So the command is nothing or the button is useless.
Use a lambda expression command=lambda: self.fnUpdateProgress(24))
if you don't add self it will be local to the function only. To access ot outside, it would have to be declared global, which is the point to avoid while using classes

Why can I not type in my Tkinter entry field [duplicate]

tl;dr: When the application calls tkinter.filedialog, entry fields do not properly focus.
Long explanation:
When initializing a tkinter application, the entry fields are enabled by default. Their state is tk.ENABLED, they can be focused on by scrolling through fields with tab, and, most importantly, they can be clicked on to select the field.
For some reason, this behavior is broken by calling tkinter.filedialog. If a method of tkinter.filedialog is called, such as askdirectory or askopenfile(), the entry field will still have the tk.ENABLED state, and the background will be properly styled, but clicking on the entry field will not insert the cursor or select the field. Typing, of course, does not register.
This can be worked around by toggling to a different window and toggling back. However, the file dialog windows (properly) return the user directly back to the main window, and so users are always presented with a main window that appears to be locked up.
See this example:
import tkinter as tk
from tkinter import filedialog
BR8K = True
root = tk.Tk()
if BR8K:
filedialog.askdirectory()
entry = tk.Entry(root, takefocus=True, highlightthickness=2)
entry.grid(sticky="WE")
root.mainloop()
Here, the code behaves properly if BR8K is False, and incorrectly if BR8K is True.
(Note: In a production environment, this would be object oriented. The issue persists in an object oriented environment.)
This is a known issues resulting from a dialog window being called prior to the mainloop() being reached for the first time.
The simplest way to fix this is to add update_idletask() before the dialog.
Try this:
import tkinter as tk
from tkinter import filedialog
BR8K = True
root = tk.Tk()
# By adding this you avoid the focus breaking issue of calling dialog before the mainloop() has had its first loop.
root.update_idletasks()
if BR8K:
filedialog.askdirectory()
entry = tk.Entry(root, takefocus=True, highlightthickness=2)
entry.grid(sticky="WE")
root.mainloop()

Tkinter text widget - Why does INSERT not work as text index?

I have a problem that annoys me. I am currently building a small app with a Tkinter GUI.
On the front page, I want some introductory text in either a text or a scrolledtext widget. The code examples I've come across uses keywords such as INSERT, CURRENT and END for indexation inside the widget.
I have literally copy pasted the below code into my editor, but it doesn't recognise INSERT (throws error: "NameError: name 'INSERT' is not defined"):
import tkinter as tk
from tkinter import scrolledtext
window = tk.Tk()
window.title("test of scrolledtext and INSERT method")
window.geometry('350x200')
txt = scrolledtext.ScrolledText(window,width=40,height=10)
txt.insert(INSERT,'You text goes here')
txt.grid(column=0,row=0)
window.mainloop()
I can get the code to work if I change [INSERT] with [1.0], but it is very frustrating that I cannot get INSERT to work, as I've seen it in every example code I've come across
Use tk.INSERT instead of only INSERT. Full code is shown.
import tkinter as tk
from tkinter import scrolledtext
window = tk.Tk()
window.title("test of scrolledtext and INSERT method")
window.geometry('350x200')
txt = scrolledtext.ScrolledText(window,width=40,height=10)
txt.insert(tk.INSERT,'You text goes here')
txt.grid(column=0,row=0)
window.mainloop()
You don't need to use the tkinter constants. I personally think it's better to use the raw strings "insert", "end", etc. They are more flexible.
However, the reason the constants don't work for you is that you're not directly importing them. The way you're importing tkinter, you need to use tk.INSERT, etc.
INSERT could not be used directly.
You can use it in the past just because you used this in the past:
from tkinter import * # this is not a good practice
INSERT,CURRENT and END are in tkinter.constants.Now in your code,you even didn't import them.
If you want to use them,you can use
from tkinter.constants import * # not recommended
...
txt.insert(INSERT,'You text goes here')
Or
from tkinter import constants
...
txt.insert(constants.INSERT,'You text goes here') # recommend
If didn't want to import them,you can also use:
txt.insert("insert",'You text goes here')
Edit:I found in the source code of tkinter,it had import them,reboot's answer is also OK.

How to set focus in tkinter entry box

have spent a while looking for an answer. I'm new to Python but not to coding in general. Finding the various versions quite challenging!
Anyway I'm very Gui orientated and have managed to get tkinter working with Python 3.5.1
Just playing with basics and have the following code but cannot set focus to the first entry box. Have tried mEntry1.focus() and mEntry1.focus_set() but always get object has no attribute error. Any help?
from tkinter import *
def calc(*args):
try:
value1 = float(V1.get())
value2 = float(V2.get())
result.set(value1 * value2)
except ValueError:
pass
mGui = Tk()
mGui.geometry('450x450+200+200')
mGui.title('Test Gui')
V1 = StringVar()
V2 = StringVar()
result = StringVar()
mEntry1 = Entry(textvariable=V1,width=10).grid(row=0,column=0,sticky=W)
mEntry2 = Entry(textvariable=V2).grid(row=1,column=0)
mButton = Button(text='Calculate',command=calc).grid(row=3,column=0)
mlabel = Label(textvariable=result).grid(row=4,column=2)
Every Tkinter widget has the focus_set method.
The problem with your code is that the .grid method returns None. Thus
mEntry1 = Entry(textvariable=V1,width=10).grid(row=0,column=0,sticky=W)
sets mEntry1 to None, not the widget. So you need to create the widget and put it in the grid in two steps:
mEntry1 = Entry(textvariable=V1,width=10)
mEntry1.grid(row=0,column=0,sticky=W)
Of course, if you don't actually need a reference to the widget object then it's ok to do it in one step. So something like
Entry(textvariable=V1,width=10).grid(row=0,column=0,sticky=W)
would be fine.
BTW, it's not a good idea to use from tkinter import *. It imports over 130 names into your namespace, which can lead to name collisions with your own names, or with those of other modules if you also import them with a "star" import statement. It also makes the code harder to read. Instead, you can do
import tkinter as tk
and then refer to Tkinter names using the tk. prefix, eg tk.Entry instead of Entry.

Categories

Resources