I have a tkinter GUI code that executes different functions which are mapped to different buttons/widgets, I want to give users the option of choosing to open the GUI window or directly execute from the command line.
I tried keeping mainloop() inside an optional switch like:
if gui == "1":
root.mainloop()
else:
#command mode
# call required functions here using user switches.
Is this the correct way of doing this? this is working when i tried in linux system.
root = Tk()
root.title('ATM v.keypad buttons //////dont acc work///////')
# creates the buttons adds no value tho
# size of button
gridlabel1 = Button(root, text='1', padx=50, pady=50)
gridlabel2 = Button(root, text='2', padx=50, pady=50)
gridlabel3 = Button(root, text='3', padx=50, pady=50)
gridlabel4 = Button(root, text='4', padx=50, pady=50)
gridlabel5 = Button(root, text='5', padx=50, pady=50)
gridlabel6 = Button(root, text='6', padx=50, pady=50)
gridlabel7 = Button(root, text='7', padx=50, pady=50)
gridlabel8 = Button(root, text='8', padx=50, pady=50)
gridlabel9 = Button(root, text='9', padx=50, pady=50)
gridlabel0 = Button(root, text='0', padx=50, pady=50)
cancelbutton = Button(root, text='cancel', padx=75, pady=50)
clearbutton = Button(root, text='clear', padx=75, pady=50)
enterbutton = Button(root, text='enter', padx=75, pady=50)
# prints the button at set location
gridlabel1.grid(row=0, column=0)
gridlabel2.grid(row=0, column=1)
gridlabel3.grid(row=0, column=2)
gridlabel4.grid(row=1, column=0)
gridlabel5.grid(row=1, column=1)
gridlabel6.grid(row=1, column=2)
gridlabel7.grid(row=2, column=0)
gridlabel8.grid(row=2, column=1)
gridlabel9.grid(row=2, column=2)
gridlabel0.grid(row=3, column=1)
cancelbutton.grid(row=0, column=4)
clearbutton.grid(row=1, column=4)
enterbutton.grid(row=2, column=4)
root.mainloop()
that was my example notice how I put root.mainloop() at the end
was indented because it was in a if statement
Related
I've made a really simple desktop app using Tkinter. When I set up everything in py2app the program looks like its generated fine, but when I click on it to open my computer restarts everytime. The code will run fine when I run it out of the Python IDE, but my screen goes black and restarts and brings me back to the MacOS login page when I try to run it by clicking on the icon or running the py2app version from the terminal. Has anyone run into this issue?
from tkinter import *
import tkinter
# importing everyting from tkinter
# expression to access among all the functions
expression = ""
# functions
def input_number(number, equation):
# accessing the global expression variable
global expression
# concatenation of string
expression = expression + str(number)
equation.set(expression)
def clear_input_field(equation):
global expression
expression = ""
# setting empty string in the input field
equation.set("Enter the expression")
def evaluate(equation):
global expression
# trying to evaluate the expression
try:
result = str(eval(expression))
# showing the result in the input field
equation.set(result)
# setting expression to empty string
expression = ""
except:
# some error occured
# showing it to the user equation.set("Enter a valid expression")
expression = ""
def new_calc():
new_window = Tk()
new_window.title("A whole new window")
# set the configuration of GUI window
new_window.geometry("450x220")
print('This is a new window!')
# creating the GUI
def main():
# main window
window = Tk()
# setting the title of GUI window
window.title("Calculator")
# set the configuration of GUI window
window.geometry("450x220")
# varible class instantiation
equation = StringVar()
# input field for the expression
input_field = Entry(window, textvariable=equation)
input_field.place(height=100)
# we are using grid position
# for the arrangement of the widgets
input_field.grid(columnspan=4, ipadx=100, ipady=5)
# settin the placeholder message for users
equation.set("Enter the expression")
# creating buttons and placing them at respective positions
_1 = Button(window, text='1', bd=0, command=lambda: input_number(1, equation), height=2, width=7)
_1.grid(row=2, column=0)
_2 = Button(window, text='2', bd=0, command=lambda: input_number(2, equation), height=2, width=7)
_2.grid(row=2, column=1)
_3 = Button(window, text='3', bd=0, command=lambda: input_number(3, equation), height=2, width=7)
_3.grid(row=2, column=2)
_4 = Button(window, text='4', bd=0, command=lambda: input_number(4, equation), height=2, width=7)
_4.grid(row=3, column=0)
_5 = Button(window, text='5', bd=0, command=lambda: input_number(5, equation), height=2, width=7)
_5.grid(row=3, column=1)
_6 = Button(window, text='6', bd=0, command=lambda: input_number(6, equation), height=2, width=7)
_6.grid(row=3, column=2)
_7 = Button(window, text='7', bd=0, command=lambda: input_number(7, equation), height=2, width=7)
_7.grid(row=4, column=0)
_8 = Button(window, text='8', bd=0, command=lambda: input_number(8, equation), height=2, width=7)
_8.grid(row=4, column=1)
_9 = Button(window, text='9', bd=0, command=lambda: input_number(9, equation), height=2, width=7)
_9.grid(row=4, column=2)
_0 = Button(window, text='0', bd=0, command=lambda: input_number(0, equation), height=2, width=7)
_0.grid(row=5, column=0)
plus = Button(window, text='+', bd=0, command=lambda: input_number('+', equation), height=2, width=7)
plus.grid(row=2, column=3)
minus = Button(window, text='-',bd=0, command=lambda: input_number('-', equation), height=2, width=7)
minus.grid(row=3, column=3)
multiply = Button(window, text='*', bd=0, command=lambda: input_number('*', equation), height=2, width=7)
multiply.grid(row=4, column=3)
divide = Button(window, text='/', bd=0, command=lambda: input_number('/', equation), height=2, width=7)
divide.grid(row=5, column=3)
equal = Button(window, text='=', bd=0, command=lambda: evaluate(equation), height=2, width=7)
equal.grid(row=5, column=2)
clear = Button(window, text='Clear', bd=0, command=lambda: clear_input_field(equation), height=2, width=7)
clear.grid(row=5, column=1)
# showing the GUI
window.mainloop()
# start of the program
if __name__ == '__main__':
main()
Here is the setup file:
from setuptools import Extension, setup
APP = ['CalcApp.py']
DATA_FILES = []
OPTIONS = {
'iconfile':'Calculator.icns',
'argv_emulation': True,
'packages': ['certifi'],
}
setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
For my project, I need to create a GUI using Tkinter. I wanted to divide it into 4 different frames, each with different background colors. However, when I do this, only the first frame's background color is shown. I attached my code below as well as a screenshot of the output I'm getting.
from Tkinter import *
import tkMessageBox
root = Tk()
root.geometry("950x800")
root.configure(background="black")
def test():
print("this is a test")
# *****Frames*****
fileFrame = Frame(root)
fileFrame.configure(background="yellow")
fileFrame.pack()
attributeFrame = Frame(root)
attributeFrame.configure(background="red")
attributeFrame.pack()
constraintFrame = Frame(root)
constraintFrame.configure(background="purple")
constraintFrame.pack()
preferenceFrame = Frame(root)
preferenceFrame.configure(background="blue")
preferenceFrame.pack()
# *****File Frame*****
label_1 = Label(fileFrame, text="Enter Attributes file name:", anchor="e",
bg="red", font="Times 25", width=25, height=1)
label_2 = Label(fileFrame, text="Enter hard constraints file name:",
anchor="e", bg="blue", fg="yellow", font="Times 25", width=25, height=1)
label_3 = Label(fileFrame, text="Enter preferences file name:",
anchor="e", bg="purple", fg="green", font="times 25", width=25, height=1)
entry_1 = Entry(fileFrame, font="Times 25")
entry_2 = Entry(fileFrame, font="Times 25")
entry_3 = Entry(fileFrame, font="Times 25")
button_1 = Button(fileFrame, text="Submit", bg="red", font="Times 20")
button_2 = Button(fileFrame, text="Submit", bg="blue", fg="yellow",
font="Times 20")
button_3 = Button(fileFrame, text="Submit", bg="purple", fg="green",
font="Times 20")
label_1.grid(row=0, padx=5, pady=5)
entry_1.grid(row=0, column=1)
button_1.grid(row=0, column=2, padx=5, pady=5)
label_2.grid(row=1, padx=5, pady=5)
entry_2.grid(row=1, column=1)
button_2.grid(row=1, column=2, padx=5, pady=5)
label_3.grid(row=2, padx=5, pady=5)
entry_3.grid(row=2, column=1)
button_3.grid(row=2, column=2, padx=5, pady=5)
# *****Attribute Frame*****
label_Attribute_header = Label(attributeFrame, text="Attributes:",
bg="red", font="Times 25", width=25, height=1)
label_Attribute_header.pack()
# *****Constraint Frame*****
label_Constraint_header = Label(constraintFrame, text="Hard Constraints:",
bg="purple", fg="green", font="Times 25", width=25, height=1)
label_Constraint_header.pack()
# *****Preference Frame*****
label_Preference_header = Label(preferenceFrame, text="Preferences:",
bg="blue", fg="yellow", font="Times 25", width=25, height=1)
label_Preference_header.pack()
root.mainloop()
I expect there to be 4 different frames, all stacked on top of each other with different background colors, but instead only the first frame has a background color. Can somebody explain to me why this is and how I should go about fixing this? Thanks
Your frames are there. The bottom three frames have fewer things in them and you haven't given them any padding. The frame shrinks to fit, so when you just have one item, you won't see the frames.
You can easily see the frames if you do one of two things:
First, you can request that the frames fill their parent window in the x direction. When you do this, you'll see them:
fileFrame.pack(fill="x")
attributeFrame.pack(fill="x")
constraintFrame.pack(fill="x")
preferenceFrame.pack(fill="x")
Second, instead of or in addition to that, you can give padding around the labels in the bottom frames. That will let the frame colors appear.
label_Attribute_header.pack(padx=20, pady=20)
...
label_Constraint_header.pack(padx=20, pady=20)
...
label_Preference_header.pack(padx=20, pady=20)
When opened normally
Fullscreen
i'm working on my first ever project, and i'm using python/tkinter. i've come up with this design(above) but i can't make it responsive.
i want the bottom section to stay the same but i want to extend the top part through the bottom.
from tkinter import *
root = Tk()
topFrame = Frame(root)
topFrame.pack(side=TOP, fill=BOTH)
scrollbar = Scrollbar(topFrame)
scrollbar.pack(side=RIGHT, fill=Y)
listbox = Listbox(topFrame)
listbox.pack(fill=BOTH)
for i in range(100):
listbox.insert(END, i)
#attaching list and scroll
listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview)
#bottom frame
bottomFrame = Frame(root)
bottomFrame.pack(side=BOTTOM, fill=X)
#bottom laft frame
b_leftFrame = Frame(bottomFrame)
b_leftFrame.pack(side=LEFT, fill=X)
#defining label
label1 = Label(b_leftFrame, text="Title")
label1.grid(row=0, column=0, sticky=E)
label2 = Label(b_leftFrame, text="Author")
label2.grid(row=0, column=2, sticky=E)
label3 = Label(b_leftFrame, text="Publisher")
label3.grid(row=0, column=4, sticky=E)
label4 = Label(b_leftFrame, text="Year")
label4.grid(row=0, column=6, sticky=E)
label5 = Label(b_leftFrame, text="Translator")
label5.grid(row=0, column=8, sticky=E)
#defining labels
title_text =StringVar()
entry1 = Entry(b_leftFrame, textvariable=title_text)
entry1.grid(row=0, column=1)
author_text =StringVar()
entry2 = Entry(b_leftFrame, textvariable=author_text)
entry2.grid(row=0, column=3)
publisher_text =StringVar()
entry3 = Entry(b_leftFrame, textvariable=publisher_text)
entry3.grid(row=0, column=5)
year_text =StringVar()
entry4 = Entry(b_leftFrame, textvariable=year_text)
entry4.grid(row=0, column=7)
translator_text =StringVar()
entry4 = Entry(b_leftFrame, textvariable=translator_text)
entry4.grid(row=0, column=9)
#bottom right
b_rightFrame = Frame(bottomFrame)
b_rightFrame.pack(side=RIGHT)
#buttons
b1=Button(b_rightFrame, text="View All", width=12)
b1.grid(row=2, column=3)
b1=Button(b_rightFrame, text="Search Entry", width=12)
b1.grid(row=3, column=3)
b1=Button(b_rightFrame, text="Add Entry", width=12)
b1.grid(row=4, column=3)
b1=Button(b_rightFrame, text="Update selected", width=12)
b1.grid(row=5, column=3)
b1=Button(b_rightFrame, text="Delete selected", width=12)
b1.grid(row=6, column=3)
b1=Button(b_rightFrame, text="Close", width=12)
b1.grid(row=7, column=3)
root.mainloop()
The problem here is that the topFrame widget, which contains the Listbox, is not taking all the available space.
Try replacing your:
topFrame.pack(fill=BOTH)
with:
topFrame.pack(fill=BOTH, expand=YES)
This will make the topFrame expand once you maximize the window.
Also, in order to make the listbox expand as well, replace your
listbox.pack(fill=BOTH)
with:
listbox.pack(fill=BOTH, expand=YES)
So my intention is when the user clicks one of the buttons, for the rest of the other buttons not to perform anything even when clicked, so basically to stop the buttons from performing their commands when the user clicks them ONLY if they have clicked in another button previously, I don't know if I expressed myself well so I am sorry if I dind't but here's my code:
from tkinter import *
import random
screen = Tk()
ticket = random.randint(1,3)
def test():
def test1():
if ticket == button1:
button_1 = Button(screen, text="RIP", fg="white", bg="red", width=15, height=2)
button_1.grid(row=0, column=0, sticky="w")
else:
button_2 = Button(screen, text="+1", fg="white", bg="green", width=15, height=2)
button_2.grid(row=0, column=0, sticky="w")
def test2():
if ticket == button2:
button_3 = Button(screen, text="RIP", fg="white", bg="red", width=15, height=2)
button_3.grid(row=0, column=1, sticky="w")
else:
button_4 = Button(screen, text="+1", fg="white", bg="green", width=15, height=2)
button_4.grid(row=0, column=1, sticky="w")
def test3():
if ticket == button3:
button_5 = Button(screen, text="RIP", fg="white", bg="red", width=15, height=2)
button_5.grid(row=0, column=2, sticky="w")
else:
button_6 = Button(screen, text="+1", fg="white", bg="green", width=15, height=2)
button_6.grid(row=0, column=2, sticky="w")
ticket = random.randint(1,3)
button1 = Button(screen, text="1", fg="white", bg="blue", width=15, height=2, command=test1)
button1.grid(row=0, column=0, sticky="w")
button1 = 1
button2 = Button(screen, text="2", fg="white", bg="blue", width=15, height=2, command=test2)
button2.grid(row=0, column=1, sticky="w"+"e"+"n"+"s")
button2 = 2
button3 = Button(screen, text="3", fg="white", bg="blue", width=15, height=2, command=test3)
button3.grid(row=0, column=2, sticky="e")
button3 = 3
button1 = Button(screen, text="START", fg="black", bg="orange", width=25, height=2, command=test)
button1.grid(row=1, columnspan=3, sticky="w"+"e"+"n"+"s")
screen.mainloop()
You can set the state of the other Buttons to DISABLED to grey them out and prevent clicks. This would be the ideal place to use a subclass that keeps track of its instances.
from tkinter import *
import random
screen = Tk()
class MykoButton(Button):
instances = []
def __init__(self, master=None, **kwargs):
super().__init__(master, command=self.run, **kwargs)
self.instances.append(self)
def run(self):
for button in self.instances:
if button is not self:
button.config(state=DISABLED)
if random.randint(1,3) == 1:
self.config(text="RIP", fg="white", bg="red") # update the button
else:
self.config(text="+1", fg="white", bg="green")
def test():
for i in range(3):
button = MykoButton(screen, text="1", fg="white", bg="blue", width=15, height=2)
button.grid(row=0, column=i)
button1 = Button(screen, text="START", fg="black", bg="orange", width=25, height=2, command=test)
button1.grid(row=1, columnspan=3, sticky="w"+"e"+"n"+"s")
screen.mainloop()
Also, note that I changed your code to update the clicked button, rather than put a new button on top of it.
I have been doing a python app for a calculator with Tkinter. I want to know how access the buttons properties such as their size, height, width and color by the use of strings. So far I have been able to change the background of the lines behind the buttons to red by using style.configure("TButton", background='red',
font="Serif 15",
padding=10 ).
How do I change the buttons themselves? If anyone could help me I would greatly appreciate it.
from tkinter import *
from tkinter.ttk import *
from tkinter import ttk
class Calculator:
calc_value = 0.0
div_trigger = False
mult_trigger = False
add_trigger = False
sub_trigger = False
def __init__(self,root):
self.entry_value = StringVar(root,value="")
root.title("Calculator")
root.geometry("600x500")
root.resizable(width=True, height=True)
style = ttk.Style()
style.configure(self, background='red')
style.configure("TButton", #background='red',
font="Serif 15",
padding=10 )
style.configure("TEntry",
font="Serif 15",
padding=10 )
self.number_entry = ttk.Entry(root,
textvariable=self.entry_value,width=50)
self.number_entry.grid(row=0, columnspan=4)
#-------1st row---------
self.button7 = ttk.Button(root, text="7",
command=lambda: self.button_press('7')).grid(row=1, column=0)
self.button8 = ttk.Button(root, text="8",
command=lambda: self.button_press('8')).grid(row=1, column=1)
self.button9 = ttk.Button(root, text="9",
command=lambda: self.button_press('9')).grid(row=1, column=2)
self.button_div = ttk.Button(root, text="/",
command=lambda: self.button_press('/')).grid(row=1, column=3)
#-------2nd row---------
self.button4 = ttk.Button(root, text="4",
command=lambda: self.button_press('4')).grid(row=2, column=0)
self.button5 = ttk.Button(root, text="5",
command=lambda: self.button_press('5')).grid(row=2, column=1)
self.button6 = ttk.Button(root, text="6",
command=lambda: self.button_press('6')).grid(row=2, column=2)
self.button_mult = ttk.Button(root, text="*",
command=lambda: self.button_press('*')).grid(row=2, column=3)
#-------3rd row---------
self.button1 = ttk.Button(root, text="1",
command=lambda: self.button_press('1')).grid(row=4, column=0)
self.button2 = ttk.Button(root, text="2",
command=lambda: self.button_press('2')).grid(row=4, column=1)
self.button3 = ttk.Button(root, text="3",
command=lambda: self.button_press('3')).grid(row=4, column=2)
self.button_add = ttk.Button(root, text="+",
command=lambda: self.button_press('+')).grid(row=4, column=3)
#-------4th row---------
self.button_clear = ttk.Button(root, text="AC",
command=lambda: self.button_press('AC')).grid(row=5, column=0)
self.button0 = ttk.Button(root, text="0",
command=lambda: self.button_press('0')).grid(row=5, column=1)
self.button_equal = ttk.Button(root, text="=",
command=lambda: self.button_press('=')).grid(row=5, column=2)
self.button_sub = ttk.Button(root, text="-",
command=lambda: self.button_press('-')).grid(row=5, column=3)
root = Tk()
calc = Calculator(root)
root.mainloop()
#button1 = Button(topframe,padx=16, pady=16, bd=8, text="1", fg="black", bg = random.choice(colors))
Here are documentatiopn about that.
http://effbot.org/tkinterbook/button.htm
If you want to change a botton size you can use.
button1.config(height = 100, width = 100)