Is there a way to create GUI component in Python Tkinter? - python

I am writing GUI in Python using Tkinter and I am having some difficulties. I have this repeatable segment:
It repeats 8 times.
I would like to create class or struct of:
frame1 = LabelFrame(root, text="Host 0", padx=5, pady=5)
frame1.grid(row=0, column=0)
labela1 = Label(frame1, text="ID 21")
c1 = Checkbutton(frame1, text="Save images")
c1.grid(row=2, column=1, columnspan=2)
b11 = Button(frame1, text="Start host")
b12 = Button(frame1, text="Start app")
b13 = Button(frame1, text="Kill app")
b14 = Button(frame1, text="Turn off host")
labela1.grid(row=1, column=0)
b11.grid(row=1, column=2)
b12.grid(row=1, column=3)
b13.grid(row=1, column=4)
b14.grid(row=1, column=5)
labela12 = Label(frame1, text="Status", fg='#00f')
labela12.grid(row=2, column=3, columnspan=4)
and then populate GUI through for loop.
Is this possible in python?
I really couldn't find anything on the web for this type of problem.
Thanks in advance!

The most common and easiest way of creating a custom widget is to start by creating a class that inherits from Frame or LabelFrame. Put anything you want inside that class. You can then use that class just like any other widget.
import tkinter as tk
class HostController(tk.LabelFrame):
def __init__(self, parent, hostid, title):
super().__init__(parent, text=title)
self.hostid = hostid
labela1 = tk.Label(self, text=f"ID {hostid}")
c1 = tk.Checkbutton(self, text="Save images")
c1.grid(row=2, column=1, columnspan=2)
b11 = tk.Button(self, text="Start host")
b12 = tk.Button(self, text="Start app")
b13 = tk.Button(self, text="Kill app")
b14 = tk.Button(self, text="Turn off host")
labela1.grid(row=1, column=0)
b11.grid(row=1, column=2)
b12.grid(row=1, column=3)
b13.grid(row=1, column=4)
b14.grid(row=1, column=5)
labela12 = tk.Label(self, text="Status", fg='#00f')
labela12.grid(row=2, column=3, columnspan=4)
root = tk.Tk()
for i in range(5):
hc = HostController(root, hostid=i, title=f"Host {i}")
hc.pack(side="top", padx=4, pady=(0,4))
root.mainloop()

Related

calling functions in one another in python

i am learning python and got stuck in a place where i want to call the first function in the second function and second function into first function on clicking a button in tkinter but due to sequencing in python i am not able to do this. plz ignore the ending part after functions.
from tkinter import *
log = Tk()
log.config(bg="grey")
log.resizable(False, False)
log.title("this is gui python")
log.geometry("800x500")
log.resizable(False, False)
frame = LabelFrame(log, bg="blue", width=200, height=500, padx=0, pady=0).place(x=0, y=0)
def sign_up():
global log
log.config(bg="grey")
log.resizable(False, False)
log.title("this is gui python")
log.geometry("800x500")
log.resizable(False, False)
frame = LabelFrame(log, bg="blue", width=200, height=500, padx=0, pady=0).place(x=0, y=0)
B1 = Button(frame, text="login window", width=27, bg="violet", pady=10, command=signin_window).place(x=1, y=0)
B2 = Button(frame, text="sign up window", width=27, bg="violet", pady=10).place(x=1, y=0)
def signin_window():
global log
log.config(bg="grey")
log.resizable(False, False)
log.title("this is gui python")
log.geometry("800x500")
log.resizable(False, False)
frame = LabelFrame(log, bg="blue", width=200, height=500, padx=0, pady=0).place(x=0, y=0)
B1 = Button(frame, text="login window", width=27, bg="violet", pady=10, relief=FLAT,).place(x=1, y=0)
B2 = Button(frame, text="sign up window", width=27, bg="violet", pady=10, relief=FLAT, command=sign_up).place(x=1, y=50)
B1 = Button(frame, text="login window", width=27, bg="violet", relief=FLAT, pady=10, command=signin_window).place(x=1, y=0)
B2 = Button(frame, text="sign up window", width=27, bg="violet", relief=FLAT, pady=10).place(x=1, y=50)
l1 = Label(log, text="welcome to login window", font="haventica 30 bold", bg="grey").place(x=250, y=110)
l2 = Label(log, text="email", font="haventica 20", bg="grey").place(x=280, y=170)
l3 = Label(log, text="password", font="haventica 20", bg="grey").place(x=280, y=220)
e1 = Entry(log, width=40, borderwidth=3)
e1.place(x=430, y=170)
e2 = Entry(log, width=40, borderwidth=3)
e2.place(x=430, y=220)
log.mainloop()
in this part in the sign_up() function you did this:
B1 = Button(frame, text="login window", width=27, bg="violet", pady=10, command=signin_window).place(x=1, y=0)
where you assigned the command "signin_window()" to it.
do you see? you used the command signin_window() before making it!
so just switch the 2 commands around and you are done.
hope it was helpful.

How to Integrate my python code to my tkinter interface?

I have made a program where the user enter's a target number and 4 other numbers to make that target number. Right now I am having trouble to intergrate my tkinter interface to my code. So I am hoping that some one can help me
Tkinter interface:
window = Tk()
window.title("target number solution")
Label(window,image='', bg="white") .grid(row=0, column=0, sticky=W)
Label(window, text="Enter target number:",bg="black", fg="white", font="none 12 bold").grid(row=1, column=0, sticky=N)
textentry = Entry(window, width=20, bg="white")
textentry.grid(row=2, column=0, sticky=N)
Label(window, text="Enter first number:",bg="black", fg="white", font="none 12 bold").grid(row=4, column=0, sticky=N)
textentry = Entry(window, width=20, bg="white")
textentry.grid(row=5, column=0, sticky=N)
Label(window, text="Enter second number:",bg="black", fg="white", font="none 12 bold").grid(row=6, column=0, sticky=N)
textentry = Entry(window, width=20, bg="white")
textentry.grid(row=7, column=0, sticky=N)
Label(window, text="Enter third number:",bg="black", fg="white", font="none 12 bold").grid(row=8, column=0, sticky=N)
textentry = Entry(window, width=20, bg="white")
textentry.grid(row=9, column=0, sticky=N)
Label(window, text="Enter fourth number:",bg="black", fg="white", font="none 12 bold").grid(row=10, column=0, sticky=N)
textentry = Entry(window, width=20, bg="white")
textentry.grid(row=11, column=0, sticky=N)
Button(window, text="Solve", width=6, command=solver).grid(row=12, column=0, sticky=N)
output = Text(window, width=60, height=10, wrap=WORD, background="white")
output.grid(row=13, column=0, columnspan=1, sticky=N)
window.mainloop()
This is the code that needs to be linked with the tkinter interface:
from itertools import permutations,combinations_with_replacement
numbers = []
target = int(input())
operators = ["+","-","*","/"]
groups = ['X+X+X+X', 'X+X+(X+X)', 'X+(X+X)+X', '(X+X+X)+X', '(X+X)+X+X', 'X+(X+X+X)', '((X+X)+X)+X', 'X+(X+(X+X))', 'X+((X+X)+X)', '(X+X)+(X+X)', '(X+(X+X))+X']
seen = set()
for values in permutations(numbers,len(numbers)):
for operCombo in combinations_with_replacement(operators,len(numbers)-1):
for oper in permutations(operCombo,len(numbers)-1):
formulaKey = "".join(oper+values)
if formulaKey in seen: continue # ignore variations on parentheses alone
for pattern in groups:
formula = "".join(o+p for o,p in zip([""]+list(oper), pattern.split("+")))
formula = "".join(v+p for v,p in zip([""]+list(values),formula.split("X")))
try:
if eval(formula) == target:
global Answer
Answer = formula,"=",target
print(formula,"=",target)
seen.add(formulaKey)
break
except: pass
All suggestions will be grealty appriciated
You can store the values for each number in seperate IntVar variables, then get the values of these variables inside the solver function and perform all the operations that are in your second code. The code should be modularised with the use of a class containing functions that create the widgets and run the solver code. To read more about class-based declarations in Tkinter, read this.
from tkinter import Tk, Frame, Label, Button, IntVar, Entry, Text, W, N, WORD, INSERT
from itertools import permutations,combinations_with_replacement
class Application(Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.create_widgets()
def create_widgets(self):
self.target_num = IntVar()
self.num1 = IntVar()
self.num2 = IntVar()
self.num3 = IntVar()
self.num4 = IntVar()
# self.title("target number solution")
Label(self,image='', bg="white").grid(row=0, column=0, sticky=W)
Label(self, text="Enter target number:", bg="black", fg="white", font="none 12 bold").grid(row=1, column=0, sticky=N)
self.textentry1 = Entry(self, textvariable=self.target_num, width=20, bg="white")
self.textentry1.grid(row=2, column=0, sticky=N)
Label(self, text="Enter first number:",bg="black", fg="white", font="none 12 bold").grid(row=4, column=0, sticky=N)
self.textentry2 = Entry(self, textvariable=self.num1, width=20, bg="white")
self.textentry2.grid(row=5, column=0, sticky=N)
Label(self, text="Enter second number:",bg="black", fg="white", font="none 12 bold").grid(row=6, column=0, sticky=N)
self.textentry3 = Entry(self, textvariable=self.num2, width=20, bg="white")
self.textentry3.grid(row=7, column=0, sticky=N)
Label(self, text="Enter third number:",bg="black", fg="white", font="none 12 bold").grid(row=8, column=0, sticky=N)
self.textentry3 = Entry(self, textvariable=self.num3, width=20, bg="white")
self.textentry3.grid(row=9, column=0, sticky=N)
Label(self, text="Enter fourth number:",bg="black", fg="white", font="none 12 bold").grid(row=10, column=0, sticky=N)
self.textentry4 = Entry(self, textvariable=self.num4, width=20, bg="white")
self.textentry4.grid(row=11, column=0, sticky=N)
Button(self, text="Solve", width=6, command=self.solver).grid(row=12, column=0, sticky=N)
self.output = Text(self, width=60, height=10, wrap=WORD, background="white")
self.output.grid(row=13, column=0, columnspan=1, sticky=N)
def solver(self):
target = self.target_num.get()
number1 = self.num1.get()
number2 = self.num2.get()
number3 = self.num3.get()
number4 = self.num4.get()
numbers = [number1, number2, number3, number4]
operators = ["+","-","*","/"]
groups = ['X+X+X+X', 'X+X+(X+X)', 'X+(X+X)+X', '(X+X+X)+X', '(X+X)+X+X', 'X+(X+X+X)', '((X+X)+X)+X', 'X+(X+(X+X))', 'X+((X+X)+X)', '(X+X)+(X+X)', '(X+(X+X))+X']
seen = set()
for values in permutations(numbers,len(numbers)):
for operCombo in combinations_with_replacement(operators,len(numbers)-1):
for oper in permutations(operCombo,len(numbers)-1):
formulaKey = "".join(str(oper+values))
if formulaKey in seen: continue # ignore variations on parentheses alone
for pattern in groups:
formula = "".join(str(o)+str(p) for o,p in zip([""]+list(oper), pattern.split("+")))
formula = "".join(str(v)+str(p) for v,p in zip([""]+list(values),formula.split("X")))
try:
if eval(formula) == target:
Answer = formula,"=",target
print(formula,"=",target)
seen.add(formulaKey)
#insert value in output Textbox
self.output.insert(INSERT, Answer)
self.output.insert(END, '\n')
break
except: pass
root = Tk()
app = Application(master=root)
app.master.title("target number solution")
app.mainloop()
A few things that you can change/try:
Keep the "Entry" variables unique. Then extract each number using .get() command.
Use lambda in the command options of the Button as follows:
command = lambda: solver(num1, num2, num3, num4)
And if you want to display your returned number from 'solver', grid a Label in your tkinter window. Then use Label.config(text="Your number here") to show it in your tkinter window

Python Tkinter grid spacing of widgets and LablelFrames not right

I am designing a simple GUI in Python 2.7 Tkinter, but I can't get things to spread out as I want them. I have managed to get my various widgets roughly where I want them, however I can't seem to force spacing out and things are a little bunched up.
I have also tried to draw 3 LabelFrames to separate the window out, but widgets seem to fall over the LabelFrames. I am wondering how I can space this out a little better. The grid system seems to allow things to bunch up and ignores blank rows and columns as far as I can see.
from Tkinter import *
import Tkinter, Tkconstants, tkFileDialog, tkMessageBox
class FileZap():
def __init__(self, root):
root.title("TestGUI")
root.geometry("860x450")
self.topFrame = LabelFrame(root, text="Top Area")
self.topFrame.grid(row=1, column=1, rowspan=6, columnspan=7, padx=5, pady = 5, sticky="NSEW")
self.listbox1 = Listbox(root, width=50, selectmode="multiple")
self.listbox1.grid(row=3, column=2)
self.scrollbar = Scrollbar(orient=VERTICAL, command=self.listbox1.yview)
self.listbox1.config(yscrollcommand=self.scrollbar.set)
self.scrollbar.grid(row=3, column=3, sticky="ns")
self.listbox2 = Listbox(root, width=50)
self.listbox2.grid(row=3, column=4)
self.selectLabel = Label(root, text="Select a folder: ")
self.selectLabel.grid(row=3, column=1)
self.user1 = Entry(root, width="50")
self.user1.grid(row=2, column=2)
self.browse = Button(root, text="Browse")
self.browse.grid(row=2, column=3)
self.addItems = Button(root, text="Add to Selection")
self.addItems.grid(row=4, column=2)
self.clearItems = Button(root, text="Clear Selection")
self.clearItems.grid(row=4, column=4)
self.leftFrame = LabelFrame(root, text="Left Area")
self.leftFrame.grid(row=5, column=1, rowspan=6, columnspan=3, padx=5, pady = 5, sticky="NSEW")
self.replaceInLable = Label(root, text="String to replace: ")
self.replaceOutLable = Label(root, text="New string: ")
self.replaceInLable.grid(row=7, column=1)
self.replaceOutLable.grid(row=7, column=2)
self.replaceIn = Entry(root, width="20")
self.replaceOut = Entry(root, width="20")
self.replaceIn.grid(row=8, column=1)
self.replaceOut.grid(row=8, column=2)
self.replace = Button(root, text="Replace")
self.replace.grid(row=8,column=3)
self.rightFrame = LabelFrame(root, text="Right Area")
self.rightFrame.grid(row=5, column=4, rowspan=6, columnspan=3, padx=5, pady = 5, sticky="NSEW")
self.quit = Button(root, text="Exit", command=root.quit)
self.quit.grid(row=9, column=6)
root = Tkinter.Tk()
file_zap = FileZap(root)
root.mainloop()
I have tried various alterations but can't nail it! Any help would be much appreciated.
First, the columns / row adapt to there content so an empty one as a zero height/width. If you want to put space between your widgets use the padx and pady options in the .grid method. They can take either one number which will give the padding on both sides or a couple of numbers giving the padding on each side.
Secondly, if you want your widgets to be inside a LabelFrame, you need to create them with this LabelFrame as master instead of the main window.
from Tkinter import LabelFrame, Tk, Button, Label
root = Tk()
# make row 0 resize with the window
root.rowconfigure(0, weight=1)
# make column 0 and 1 resize with the window
root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=1)
# create LabelFrames
top_frame = LabelFrame(root, text="top")
left_frame = LabelFrame(root, text="left")
right_frame = LabelFrame(root, text="right")
top_frame.grid(row=0, column=0, columnspan=2, padx=10, pady=(10,4), sticky="nsew")
left_frame.grid(row=1, column=0, padx=(10,4), pady=4, sticky="nsew")
right_frame.grid(row=1, column=1, padx=(4,10), pady=4, sticky="nsew")
#create widgets inside top_frame
Label(top_frame, text="I'm inside top_frame").pack()
Button(top_frame, text="Top").pack()
#create widgets inside left_frame
Label(left_frame, text="I'm inside left_frame").pack()
Button(left_frame, text="Left").pack()
#create widgets inside top_frame
Label(right_frame, text="I'm inside right_frame").pack()
Button(right_frame, text="Right").pack()
Button(root, text="Quit", command=root.destroy).grid(row=2, column=0,
columnspan=2, pady=10)
root.mainloop()

Multiple Windows using Python with Tkinter

I have a school project and I need to create my GUI soon and I cant find an efficient way to do so and I need to make a ceaser Cipher button which brings up a window with an input in there and I need it all to look nice. Here is my code for now. Its not finished and needs to be finished but don't know how to?
from tkinter import *
root = Tk()
root.geometry("180x135")
class Welcome:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.button1 = Button(topFrame, text ="Decrypter Mode", bg='Black', fg="red", command = self.new_window)
self.button1.pack(side=RIGHT)
self.quitButton = Button(frame, bg='black', fg='white', text='Quit', command=master.destroy)
self.quitButton.pack(side=LEFT)
def new_window(self):
self.newWindow = tk.Toplevel(self.master)
self.app = Demo2(self.newWindow)
topFrame = Frame(root)
topFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack(side=BOTTOM)
one = Label(root, text='Welcome to Crypto-coder', bg='black', fg='white')
one.pack(fill=X)
one = Label(root, text='', bg='black', fg='white')
one.pack(fill=X,)
one = Label(root, text='', bg='black', fg='white')
one.pack(fill=X,)
one = Label(root, text='', bg='black', fg='white')
one.pack(fill=X,)
button1 = Button(topFrame, text ="Decrypter Mode", bg='Black', fg="red",)
button2 = Button(topFrame, text ="Encoder Mode", bg='black', fg="blue")
button1.pack(side=RIGHT)
button2.pack(side=LEFT)
W=Welcome(root)
root.mainloop()
I had to move some things for the code to show so you may have to fix it if you try it.

Can convert Tkinter inputs into numbers

__author__ = 'Feuer'
from tkinter import *
root = Tk()
root.geometry("750x400")
def stop():
exit()
def plus():
global erg11
erg11 = z1 + z2
class Start:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.Label_1 = Label(frame, text="Bitte wählen Sie die Rechenart aus, die Sie benutzen möchten!")
self.Label_2 = Label(frame, text="Addition")
self.Label_3 = Label(frame, text="Subtraktion")
self.Label_4 = Label(frame, text="Multiplikation")
self.Label_5 = Label(frame, text="Division")
self.Label_6 = Label(frame, text="Wurzel")
self.Label_7 = Label(frame, text="Logarithmus")
self.Button_1 = Button(frame, text="Go!", command=Add)
self.Button_2 = Button(frame, text="Go!")
self.Button_3 = Button(frame, text="Go!")
self.Button_4 = Button(frame, text="Go!")
self.Button_5 = Button(frame, text="Go!")
self.Button_6 = Button(frame, text="Go!")
self.Button_7 = Button(frame, text="Das Programm beenden!", command=stop)
self.Label_1.grid(row=0, columnspan=2)
self.Label_2.grid(row=1, column=0)
self.Label_3.grid(row=2, column=0)
self.Label_4.grid(row=3, column=0)
self.Label_5.grid(row=4, column=0)
self.Label_6.grid(row=5, column=0)
self.Label_7.grid(row=6, column=0)
self.Button_1.grid(row=1, column=1)
self.Button_2.grid(row=2, column=1)
self.Button_3.grid(row=3, column=1)
self.Button_4.grid(row=4, column=1)
self.Button_5.grid(row=5, column=1)
self.Button_6.grid(row=6, column=1)
self.Button_7.grid(row=7, columnspan=2)
class Add:
def __init__(self):
newwin = Toplevel()
newwin.geometry("750x400")
frame2 = Frame(newwin)
frame2.pack()
global erg11
global z1
global z2
erg11 = "Ready"
self.Label_1 = Label(frame2, text="Additionsverfahren")
self.Entry_1 = Entry(frame2)
self.Label_2 = Label(frame2, text="+")
self.Entry_2 = Entry(frame2)
self.Label_3 = Label(frame2, text="=")
self.Button_1 = Button(frame2, text="Zurück", command=newwin.destroy)
self.Button_2 = Button(frame2, text="Ergebniss berechnen")
self.Label_Erg1 = Label(frame2, text=erg11)
self.Button_2.bind("<Button-1>", plus)
self.Label_1.grid(row=0, columnspan=4)
self.Entry_1.grid(row=1, column=0)
self.Label_2.grid(row=1, column=1)
self.Entry_2.grid(row=1, column=2)
self.Label_3.grid(row=1, column=3)
self.Button_2.grid(row=2, columnspan=4)
self.Button_1.grid(row=3, columnspan=4)
self.Label_Erg1.grid(row=1, column=4)
app = Start(root)
root.mainloop()
this is my code i am using at the moment.I try to create a little useless calculator with gui in Python. I cant figure out how to get the variable (z1 / z1) out of Entry_1 and _2 when someone is pressing the button_2 (in the second class). Could any one sugesst me some code to fix it?
Edit:
I edited the Code, that everybody could try to find a solution for my problem, because my solution approaches ended in a stalemate. (Ayres)
The content of your entries are read immediately after creation of them, leading get() to return an empty string that can't be converted.
The get method has to be called somewhere else, though I can't exactly say when.

Categories