What i wanted to do is, while typing some words in Entry widget, at the same time changing the characters that are displayed in label widget. Here are the codes:
import tkinter as tk
class App(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.entry = tk.Entry(master=self)
self.entry.pack(side="left")
self.var = tk.StringVar()
self.var.set(self.entry.get)
self.label = tk.Label(master=self)
self.label.pack(side="left")
self.configure_widgets()
self.pack()
def configure_widgets(self):
self.label.configure(textvariable=self.var)
if __name__ == "__main__":
root = tk.Tk()
example = App(master=root)
example.mainloop()
Which parts i should change of the codes? Thank you in advance.
Both Entry and Label accept a variable as a parameter. The entry will set the variable value and the Label will get it.
import tkinter as tk
class App(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.var = tk.StringVar()
self.entry = tk.Entry(master=self, textvariable=self.var)
self.entry.pack(side="left")
self.label = tk.Label(master=self, textvariable=self.var)
self.label.pack(side="left")
self.configure_widgets()
self.pack()
def configure_widgets(self):
self.label.configure(textvariable=self.var)
if __name__ == "__main__":
root = tk.Tk()
example = App(master=root)
example.mainloop()
Related
I have a script, and woult like to have some imputs, outputs (as in the terminal) and a start running script.
How can I do this?
this is what I have for now:
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
exitButton = Button(self, text="Run", command=self.clickExitButton)
exitButton.place(x=0, y=0)
def clickrunButton(self):
run() #this doesnt work
root = Tk()
app = Window(root)
# set window title
root.wm_title("Tkinter window")
# show window
root.mainloop()
You have to place your app in the root, for example with pack(). You also have to change the name of the function, because it doesn't match the one you give to the button command.
from tkinter import *
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.pack() # Window is packed in its master.
exitButton = Button(self, text="Run", command=self.clickrunButton)
exitButton.pack()
def clickrunButton(self):
self.run() # Now this work
def run(self):
print('Something')
root = Tk()
app = Window(root)
# set window title
root.wm_title("Tkinter window")
# show window
root.mainloop()
I have a Tkinter app in which I would like to include some buttons in a frame, and then place this frame in the main window.
However running the code returns just an empty window. So I guess I miss completely how to build a Tkinter app with modular classes.. The atomic code is:
import Tkinter as tk
class MainApplication(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
self.navbar = NavBar(self)
self.navbar.grid(row=0, column=0)
class NavBar(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.parent = parent
self.fetchDataBtn = tk.Button(self, text='Fetch data')
self.filterDataBtn = tk.Button(self, text='Filter data')
self.fetchDataBtn.pack(padx=5, pady=10, side=tk.LEFT)
self.filterDataBtn.pack(padx=5, pady=20, side=tk.LEFT)
def main():
root = tk.Tk()
app = MainApplication(root)
root.mainloop()
if __name__ == '__main__':
main()
I thus wonder what I miss. I searched but cannot find duplicates ..(if they are some, you can point out and I will close the topic).
NB: I am using Python 2.7.10
The problem is that you don't pack (or grid or place) your MainApplication instance.
Since your MainApplication extends the tk.Frame class, its instances are widgets, and thus need to be packed into their master.
def main():
root = tk.Tk()
app = MainApplication(root) <--- here: where does it go in the root?
root.mainloop()
Pack it and it will work:
app.pack()
You must put the navbar in the parent frame, using pack, or grid:
import Tkinter as tk
class MainApplication(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
self.navbar = NavBar(self)
self.navbar.grid(row=0, column=0)
self.pack() # <-- here ---------
class NavBar(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.parent = parent
self.fetchDataBtn = tk.Button(self, text='Fetch data')
self.filterDataBtn = tk.Button(self, text='Filter data')
self.fetchDataBtn.pack(padx=5, pady=10, side=tk.LEFT)
self.filterDataBtn.pack(padx=5, pady=20, side=tk.LEFT)
def main():
root = tk.Tk()
app = MainApplication(root)
# app.pack() # <-- or here for a better control of the placement of several instances
root.mainloop()
if __name__ == '__main__':
main()
Credits to #RightLeg for pointing out an initial mistake.
I am trying to update a label, but the code I have written creates a new label each time. I am relatively new to tkinter so I couldn't understand how to apply other answers to my code.
from tkinter import *
import random
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master=master
self.init_window()
def init_window(self):
self.pack(fill=BOTH, expand=1)
testButton=Button(self, text="Press", command=calc)
testButton.pack()
l1=Label(text="")
def testbutton(ans): #creates a new instance of l1 each time, I want to update existing l1
var=StringVar()
l1=Label(textvariable=var) #l1.configure() gives error l1 not defined
var.set(ans)
l1.pack()
def calc():
list1=["a","b","c"]
index=random.randint(0,2)
answer=list1[index]
Window.testbutton(answer)
root=Tk()
root.geometry("400x300")
app=Window(root)
root.mainloop()
Each time the button is pressed, a new label is created, instead of updating the text on the existing label.
This is a simplified version of my actual project, but highlights the issue with labels.
I've tried to use l1.configure(...) inside the testbutton function but then it runs an error that l1 isn't defined.
To avoid creating a new Label each time, you need to make one and save it as an attribute of the Window instance. To make it accessible to the calc() function, you'll also need to pass the Window instance to it as an argument (to avoid using global variables). A common why to do that with tkinter is by using a lamba function as the Button's command= argument and making self the default value for its argument as shown below.
from tkinter import *
import random
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.init_window()
def init_window(self):
self.pack(fill=BOTH, expand=1)
testButton = Button(self, text="Press",
command=lambda win=self: calc(win))
testButton.pack()
self.l1 = Label(text="")
self.l1.pack()
def testbutton(self, ans):
self.l1.configure(text=ans)
def calc(window): # note window argument added
list1 = ["a","b","c"]
index = random.randint(0,2)
answer = list1[index]
window.testbutton(answer)
root = Tk()
root.geometry("400x300")
app = Window(root)
root.mainloop()
You can just use class methods and attributes.
Use a StringVar to change label text:
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.init_window()
def init_window(self):
self.pack(fill=BOTH, expand=1)
testButton = Button(self, text="Press", command=self.calc)
testButton.pack()
self.ltext = StringVar()
l1 = Label(textvariable=self.ltext)
l1.pack()
def testbutton(self, ans):
self.ltext.set(ans)
def calc(self):
list1 = ["a", "b", "c"]
index = random.randint(0, 2)
answer = list1[index]
self.testbutton(answer)
root = Tk()
root.geometry("400x300")
app = Window(root)
root.mainloop()
I'm extremely new to python and I decided to try to make some things to test how things work. But I couldn't find a way to make a text on a tkinter button take the place of 2 rows. Here's the code:
import tkinter as tk
hoho = 0
def lul():
global hoho
hoho = hoho + 1
print(hoho)
class Application(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.pack()
self.createWidgets()
def createWidgets(self):
self.hi_there = tk.Button(self, fg="green")
self.hi_there["text"] = "Pressing buttons is fun, isn't it?"
self.hi_there["command"] = self.lel
self.hi_there.pack(side="top")
def lel(self):
lul()
root = tk.Tk()
app = Application(master=root)
app.mainloop()
If you know a way please let me know!
I hope I understood the question right but you can use \n in your string to print the text on a new line on the button.
import tkinter as tk
hoho = 0
def lul():
global hoho
hoho = hoho + 1
print(hoho)
class Application(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.pack()
self.createWidgets()
def createWidgets(self):
self.hi_there = tk.Button(self, fg="green")
self.hi_there["text"] = "Pressing buttons\n is fun, isn't it?"
self.hi_there["command"] = self.lel
self.hi_there.pack(side="top")
def lel(self):
lul()
root = tk.Tk()
app = Application(master=root)
app.mainloop()
Similar questions have been asked, but none of them address the particular way my script is constructed:
from Tkinter import *
from ttk import *
class Gui(Frame):
def __init__(self, parent):
Frame.__init__(self, parent) #Gui inherits from built in Frame Class
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Shoes Ware")
self.pack(fill=BOTH, expand=1)
run_val = Entry(self)
run_val["width"] = 5
run_val.place(x=80, y=40)
quit_B = Button(self, text="Submit", command=self.submit)
quit_B.place(x=130, y=170)
def submit(self):
value = run_val.get()
print value
self.quit()
def main():
root = Tk()
root.geometry("300x200+50+50")
app = Gui(root)
root.mainloop()
if __name__ == '__main__':
main()
I get "NameError: global name 'run_val' is not defined" when I hit the submit button. What am I doing wrong here. Right now the print statement is just to check my work. Later on, I'll be using that value in a program.
You are not storing the reference to the Entry widget in initUI.
def initUI(self):
# ...
self.run_val = Entry(self)
self.run_val["width"] = 5
self.run_val.place(x=80, y=40)
Then you can retrieve the value of self.run_val.get() without any problem.