Brendan Brendan - 4 months ago 49
Linux Question

2 Buttons Overlapping Python Tkinter

I am newish to Tkinter but spent hours and could not get 2 buttons to work at once. Please help me! Thanks!

I am trying to make this program. What it does is when you click a number displayed on the screen goes down by 1. I would like to add another button in that will call a function called "upClick". I already have the function. When using both buttons the second one just does ot function at all.

Code:

from tkinter import *

import sys

root = Tk()
root.geometry("480x320")
root.title("School Days Left!")

global counter
counter = 30

def upClick():
global counter
counter += 1
mButton2.config(text = "", borderwidth = 0, highlightthickness=0, relief='ridge', pady = "100")


def downClick():
global counter
counter -= 1
mButton1.config(text = counter, borderwidth = 0, highlightthickness=0, relief='ridge', pady = "100")

mButton1 = Button(text = counter, command = downClick, height = 4000, width = 320, font = ("Monospace", 200))
mButton1.pack()

mButton2 = Button(text = "", command = downClick, height = 50, width = 50, font = ("Monospace", 10))
mButton2.pack()

root.mainloop()

Answer Source

In order to just make both buttons work at once, you can start with smaller size and font size.

If you want to have more command about where do your buttons appear, take a look at other geometry managers. Pack is somewhat limited by nature, and it seems like it won't be enough in your case.

For buttons to overlap, you can use Place manager:

def upClick():
    global counter
    counter += 1
    mButton1.config(text = counter, borderwidth = 0, highlightthickness=0, relief='ridge', pady = "100")


def downClick():
    global counter
    counter -= 1
    mButton1.config(text = counter, borderwidth = 0, highlightthickness=0, relief='ridge', pady = "100")

mButton1 = Button(text = counter, command = downClick, height = 4000, width = 320, font = ("Monospace", 200))
mButton1.pack()

mButton2 = Button(text = "", command = upClick, height = 5, width = 5, font = ("Monospace", 10))
mButton2.place(anchor="nw")