Python Tkinter, Number of Label/Entry based on IntVar - python

I was hoping someone could help me with something. I would like to make an Tkinter app that asks for a number then uses that number to draw the correct number of Labels and Entrys.
Here's basic brainstorm of what I'm trying to do (I know this is wrong)
from Tkinter import *
root = Tk()
numlines = IntVar()
Label(root, text='Number Of Lines').grid(row=0, column=0) #this is to always stay
Entry(root, textvariable=numlines).grid(row=0, column=1) #this is to stay always stay
Button(root, text='Apply Number', command=apply).grid(row=1, column=1)
def apply():
# this part to draw as a block based in numline(if munlines 5 then draw these two widget 5 times on 5 rows)
Label(root, text='Line 1').grid(row=2, column=0)# this part to draw as a block based in numline(if munlines 5 then draw these two widget 5 times)
Entry(root, textvariable=numlines).grid(row=2, column=1)
root.mainloop()

Realistically all Tkinter applications should be put inside of a class. In addition it is also general a bad idea for use import * from any package as you could have override issues with unknown values that you imported. As such, the following example is inside of a class and should give you an idea of how this would look. I believe this is what you're looking for:
import Tkinter as Tk
class App(Tk.Frame):
def __init__(self, master, *args, **kwargs):
Tk.Frame.__init__(self, *args, **kwargs)
self.existing_lines = 2
self.entry_lines = Tk.IntVar()
Tk.Label(self, text="Number Of Lines").grid(row=0, column=0)
Tk.Entry(self, textvariable=self.entry_lines).grid(row=0, column=1)
Tk.Button(self, text="Apply Number", command=self.add_rows).grid(row=1, column=1)
def add_rows(self):
for row in xrange(self.existing_lines, self.existing_lines+self.entry_lines.get()):
Tk.Label(self, text="Line %i" % (row-1)).grid(row=row, column=0)
Tk.Entry(self, textvariable=self.entry_lines).grid(row=row, column=1)
self.existing_lines+= 1
if __name__ == "__main__":
root = Tk.Tk()
App(root).pack()
root.mainloop()

Related

Is there a way to display a class in python tkinter as a Frame, so other things can be added?

I want to have a tkinter window that displays both a cronometer and a sudoku. The cronometer is a class, so how can I add it to the window that displays the sudoku?
I already managed to get two separate windows, but I couldn't make one with both things.
def GUI4x4(dif): #This function gets just called from other place
# What I want is to be able to display this class
# Cronometer in the main window that's created below
class Cronometer():
...
def __init__(self):
self.crono=Tk()
self.tiempo = StringVar()
self.tiempo.set("00:00:00")
self.label = Label(self.crono,textvariable=self.tiempo, bg="white")
self.label.grid(column=0,row=0)
self.label.configure(font=("Times 13 bold"))
self.btnI = Button(self.crono, bg="white", text="Start",command=self.iniciarT,font=("Times 11"))
self.btnI.grid(pady=3,column=0,row=1)
self.btnP = Button(self.crono, bg="white", text="Pause",command=self.pausarT,font=("Times 11"))
self.btnP.grid(pady=3,column=0,row=2)
self.btnR = Button(self.crono, bg="white", text="Restart",command=self.reiniciarT,font=("Times 11"))
self.btnR.grid(pady=3,column=0,row=3)
GUI = Tk() # This creates the main window, and places
# 34 buttons in it
...
# Defining the Buttons
btn00 = Button(GUI, text=Tablero[0][0], width=5, height=3, activebackground="lime")
btn01 = Button(GUI, text=Tablero[0][1], width=5, height=3, activebackground="lime")
btn02 = Button(GUI, text=Tablero[0][2], width=5, height=3, activebackground="lime")
...
btn33 = Button(GUI, text=Tablero[3][3], width=5, height=3, activebackground="lime")
#Placing the 34 buttons
btn00.grid(row=0, column=0)
btn01.grid(row=0, column=1)
btn02.grid(row=0, column=2)
...
btn33.grid(row=3, column=3)
The standard way to deal with this with tkinter is that each "widget" in the application is its own class based on the tkinter Frame widget, one class for the chrono, another for the sudoko game. There might even be a main app class.
Advantage of this method is that each widget frame can be created independently and then joined together later. These classes might also be split up in to separate code files.
A fairly simple example below
import tkinter as tk
class Chromometer(tk.Frame):
def __init__(self,master=None,**kw):
tk.Frame.__init__(self,master=master,**kw)
self.tiempo = tk.StringVar()
self.tiempo.set("00:00:00")
self.label = tk.Label(self,textvariable=self.tiempo, bg="white")
self.label.grid(column=0,row=0)
class Sudoko(tk.Frame):
def __init__(self,master=None,**kw):
tk.Frame.__init__(self,master=master,**kw)
self.label = tk.Label(self,text="Sudoko", bg="white")
self.label.grid(column=0,row=0)
class MainApp(tk.Frame):
def __init__(self,master=None,**kw):
tk.Frame.__init__(self,master=master,**kw)
self.chrono = Chromometer(master=self)
self.chrono.grid()
self.sudoko = Sudoko(master=self)
self.sudoko.grid()
if __name__ == '__main__':
root = tk.Tk()
app = MainApp(master=root)
app.grid()
root.mainloop()
Each class will have their own methods to perform the functionality needed by each. The chromo/chrono class will have a method to update the timer.

python 3 Tkinter ComboBox don't get value

I am trying to get the value of from a combobox in tkinter using python 3.6, i been looking to many tutorials but i don't see the problem yet.
every time i press the button don't show anything.
but also there is not errors.
so to clarify ... I am trying to get the value of the tk.combobox when i press ttk.Button.
thank you in advance for any ideas or comments.
this is what i have so far.
import tkinter as tk
from tkinter import ttk
def combo_box_updater():
total_location = ['linden', 'mineola', 'brooklyn']
return total_location
def start_analisys(event=None):
site = jobsite_name.get()
print(site)
# this is part of a definition that automatically will update the names in later versions
job_site = combo_box_updater()
# basic gui setup
unified = tk.Toplevel()
unified.title('Unified 1 Week Timesheet')
unified.configure(background="#00012f")
unified.geometry("650x200")
unified.resizable(width=False, height=False)
entry_width = 30
# basic frame
frame1 = tk.Frame(unified)
frame1.grid(row=0, column=0, sticky='w')
# combo box in the fourth row
jobsite_name = tk.StringVar()
combo_box = ttk.Combobox(frame1, font="none 12 bold", width=20, textvariable=jobsite_name, text="choose location")
combo_box.grid(row=0, column=1, sticky="wesn")
combo_box['values'] = [x for x in job_site]
# Left button side
ttk.Button(frame1, text='Run', command=start_analisys, ).grid(row=0, column=2, sticky='nsew', rowspan=3)
unified.mainloop()
Made three minor edits to your code: added a label to display the result, added a line to combo box setup, and changed the creation of the main window.
import tkinter as tk
from tkinter import ttk
def combo_box_updater():
total_location = ['linden', 'mineola', 'brooklyn']
return total_location
def start_analisys(event=None):
site = jobsite_name.get()
aLabel["text"] = site
print(site)
# this is part of a definition that automatically will update the names in later versions
job_site = combo_box_updater()
# basic gui setup
unified = tk.Tk()
unified.title('Unified 1 Week Timesheet')
unified.configure(background="#00012f")
unified.geometry("650x200")
unified.resizable(width=False, height=False)
entry_width = 30
# basic frame
frame1 = tk.Frame(unified)
frame1.grid(row=0, column=0, sticky='w')
# combo box in the fourth row
jobsite_name = tk.StringVar()
combo_box = ttk.Combobox(frame1, font="none 12 bold", width=20, textvariable=jobsite_name)
combo_box.grid(row=0, column=1, sticky="wesn")
combo_box['values'] = [x for x in job_site]
combo_box.current(0)
# Left button side
ttk.Button(frame1, text='Run', command=start_analisys, ).grid(row=0, column=2, sticky='nsew', rowspan=3)
# add a label
aLabel = ttk.Label(frame1, text='My Label')
# place the label
aLabel.grid(column=3, row=0)
unified.mainloop()
if __name__ == '__main__':
pass
When you add the values as an afterthought like that you need to add the corresponding commands as well. It's much better to add the values through the init method so the commands are automatically added:
jobsite_name = tk.StringVar(value="choose location")
combo_box = ttk.Combobox(frame1, textvariable=jobsite_name, values=job_site, font="none 12 bold", width=20)

Tkinter : Button in frame not visible

I'm trying to implement a TicTacToe program. I am an absolute beginner in python. After viewing many tutorials and reading a few books, I have understood the basics of Python. I'm trying to get the buttons to display in a frame, but all I get is a blank window.
link for image of the resultant window
This is the code I have so far:
from Tkinter import *
class Buttons(object):
def __init__(self,master):
frame = Frame(master)
frame.pack()
self.button1= Button(frame,text="1",height=4,width=8,command=self.move)
self.button1.pack(side=LEFT)
self.button2= Button(frame,text="2",height=4,width=8,command=self.move)
self.button2.pack(side=LEFT)
self.button3= Button(frame,text="3",height=4,width=8,command=self.move)
self.button3.pack(side=LEFT)
root = Tk()
root=mainloop()
You defined your Buttons class but you didn't create an instance of that class, so no buttons were actually constructed. Also, you had a typo / syntax error:
root=mainloop()
should be
root.mainloop()
Also, you didn't define the move callback method.
Here's a repaired version of your code:
from Tkinter import *
class Buttons(object):
def __init__(self,master):
frame = Frame(master)
frame.pack()
self.button1 = Button(frame, text="1", height=4, width=8, command=self.move)
self.button1.pack(side=LEFT)
self.button2 = Button(frame, text="2", height=4, width=8, command=self.move)
self.button2.pack(side=LEFT)
self.button3 = Button(frame, text="3", height=4, width=8, command=self.move)
self.button3.pack(side=LEFT)
def move(self):
print "click!"
root = Tk()
Buttons(root)
root.mainloop()
However, this still has a problem: The move method has no way of knowing which button called it. Here's one way to fix that. I've also changed
from Tkinter import *
to
import tkinter as tk
It's not a good idea to use "star" imports. They make code harder to read and they pollute your namespace with all the names defined in the imported module (that's 175 names in the case of Tkinter), which can lead to name collisions.
import Tkinter as tk
class Buttons(object):
def __init__(self,master):
frame = tk.Frame(master)
frame.pack()
self.buttons = []
for i in range(1, 4):
button = tk.Button(
frame, text=i, height=4, width=8,
command=lambda n=i:self.move(n)
)
button.pack(side=tk.LEFT)
self.buttons.append(button)
def move(self, n):
print "click", n
root = tk.Tk()
Buttons(root)
root.mainloop()
Okay the problem was I needed to add a variable at the end of the code. Something like b=Buttons(root). It's working now.

Entry data manipulation difficulties [duplicate]

I'm trying to use an Entry field to get manual input, and then work with that data.
All sources I've found claim I should use the get() function, but I haven't found a simple working mini example yet, and I can't get it to work.
I hope someone can tel me what I'm doing wrong. Here's a mini file:
from tkinter import *
master = Tk()
Label(master, text="Input: ").grid(row=0, sticky=W)
entry = Entry(master)
entry.grid(row=0, column=1)
content = entry.get()
print(content) # does not work
mainloop()
This gives me an Entry field I can type in, but I can't do anything with the data once it's typed in.
I suspect my code doesn't work because initially, entry is empty. But then how do I access input data once it has been typed in?
It looks like you may be confused as to when commands are run. In your example, you are calling the get method before the GUI has a chance to be displayed on the screen (which happens after you call mainloop.
Try adding a button that calls the get method. This is much easier if you write your application as a class. For example:
import tkinter as tk
class SampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.entry = tk.Entry(self)
self.button = tk.Button(self, text="Get", command=self.on_button)
self.button.pack()
self.entry.pack()
def on_button(self):
print(self.entry.get())
app = SampleApp()
app.mainloop()
Run the program, type into the entry widget, then click on the button.
You could also use a StringVar variable, even if it's not strictly necessary:
v = StringVar()
e = Entry(master, textvariable=v)
e.pack()
v.set("a default value")
s = v.get()
For more information, see this page on effbot.org.
A simple example without classes:
from tkinter import *
master = Tk()
# Create this method before you create the entry
def return_entry(en):
"""Gets and prints the content of the entry"""
content = entry.get()
print(content)
Label(master, text="Input: ").grid(row=0, sticky=W)
entry = Entry(master)
entry.grid(row=0, column=1)
# Connect the entry with the return button
entry.bind('<Return>', return_entry)
mainloop()
*
master = Tk()
entryb1 = StringVar
Label(master, text="Input: ").grid(row=0, sticky=W)
Entry(master, textvariable=entryb1).grid(row=1, column=1)
b1 = Button(master, text="continue", command=print_content)
b1.grid(row=2, column=1)
def print_content():
global entryb1
content = entryb1.get()
print(content)
master.mainloop()
What you did wrong was not put it inside a Define function then you hadn't used the .get function with the textvariable you had set.
you need to put a textvariable in it, so you can use set() and get() method :
var=StringVar()
x= Entry (root,textvariable=var)
Most of the answers I found only showed how to do it with tkinter as tk. This was a problem for me as my program was 300 lines long with tons of other labels and buttons, and I would have had to change a lot of it.
Here's a way to do it without importing tkinter as tk or using StringVars. I modified the original mini program by:
making it a class
adding a button and an extra method.
This program opens up a tkinter window with an entry box and an "Enter" button. Clicking the Enter button prints whatever is in the entry box.
from tkinter import *
class mini():
def __init__(self):
master = Tk()
Label(master, text="Input: ").grid(row=0, sticky=W)
Button(master, text='Enter', command=self.get_content).grid(row=1)
self.entry = Entry(master)
self.entry.grid(row=0, column=1)
master.mainloop()
def get_content(self):
content = self.entry.get()
print(content)
m = mini()

.get() not giving a value [duplicate]

I'm trying to use an Entry field to get manual input, and then work with that data.
All sources I've found claim I should use the get() function, but I haven't found a simple working mini example yet, and I can't get it to work.
I hope someone can tel me what I'm doing wrong. Here's a mini file:
from tkinter import *
master = Tk()
Label(master, text="Input: ").grid(row=0, sticky=W)
entry = Entry(master)
entry.grid(row=0, column=1)
content = entry.get()
print(content) # does not work
mainloop()
This gives me an Entry field I can type in, but I can't do anything with the data once it's typed in.
I suspect my code doesn't work because initially, entry is empty. But then how do I access input data once it has been typed in?
It looks like you may be confused as to when commands are run. In your example, you are calling the get method before the GUI has a chance to be displayed on the screen (which happens after you call mainloop.
Try adding a button that calls the get method. This is much easier if you write your application as a class. For example:
import tkinter as tk
class SampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.entry = tk.Entry(self)
self.button = tk.Button(self, text="Get", command=self.on_button)
self.button.pack()
self.entry.pack()
def on_button(self):
print(self.entry.get())
app = SampleApp()
app.mainloop()
Run the program, type into the entry widget, then click on the button.
You could also use a StringVar variable, even if it's not strictly necessary:
v = StringVar()
e = Entry(master, textvariable=v)
e.pack()
v.set("a default value")
s = v.get()
For more information, see this page on effbot.org.
A simple example without classes:
from tkinter import *
master = Tk()
# Create this method before you create the entry
def return_entry(en):
"""Gets and prints the content of the entry"""
content = entry.get()
print(content)
Label(master, text="Input: ").grid(row=0, sticky=W)
entry = Entry(master)
entry.grid(row=0, column=1)
# Connect the entry with the return button
entry.bind('<Return>', return_entry)
mainloop()
*
master = Tk()
entryb1 = StringVar
Label(master, text="Input: ").grid(row=0, sticky=W)
Entry(master, textvariable=entryb1).grid(row=1, column=1)
b1 = Button(master, text="continue", command=print_content)
b1.grid(row=2, column=1)
def print_content():
global entryb1
content = entryb1.get()
print(content)
master.mainloop()
What you did wrong was not put it inside a Define function then you hadn't used the .get function with the textvariable you had set.
you need to put a textvariable in it, so you can use set() and get() method :
var=StringVar()
x= Entry (root,textvariable=var)
Most of the answers I found only showed how to do it with tkinter as tk. This was a problem for me as my program was 300 lines long with tons of other labels and buttons, and I would have had to change a lot of it.
Here's a way to do it without importing tkinter as tk or using StringVars. I modified the original mini program by:
making it a class
adding a button and an extra method.
This program opens up a tkinter window with an entry box and an "Enter" button. Clicking the Enter button prints whatever is in the entry box.
from tkinter import *
class mini():
def __init__(self):
master = Tk()
Label(master, text="Input: ").grid(row=0, sticky=W)
Button(master, text='Enter', command=self.get_content).grid(row=1)
self.entry = Entry(master)
self.entry.grid(row=0, column=1)
master.mainloop()
def get_content(self):
content = self.entry.get()
print(content)
m = mini()

Categories