Woody_99
Posts: 7
Joined: Sat Nov 11, 2017 4:18 pm

GUI with different interfaces

Wed Nov 15, 2017 7:04 am

Hello, does any of you have an idea how you can program a GUI with Tkinter, using different interfaces (for example, different arrangement of buttons)? Or has someone already programmed something like that?
Thanks, Woody

Davies
Posts: 150
Joined: Sat Apr 04, 2015 4:24 pm

Re: GUI with different interfaces

Wed Nov 15, 2017 10:48 am

im unsure what ur wanting, you used an arrangement of buttons on your other forum post you had 2 buttons top right and one in middle of page, are you wanting buttons within their own window, or circle buttons instead of square?

scotty101
Posts: 3103
Joined: Fri Jun 08, 2012 6:03 pm

Re: GUI with different interfaces

Wed Nov 15, 2017 11:35 am

I also am not sure what the OP is asking for. Woody, perhaps you can provide a simple diagram to explain what you are looking for?
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Davies
Posts: 150
Joined: Sat Apr 04, 2015 4:24 pm

Re: GUI with different interfaces

Wed Nov 15, 2017 12:38 pm

perhaps your wanting multiple pages within 1 GUI?
this code has multiple pages that can be access by pressing "+" on the UI
it also uses threading, if your using tkinter and want background processing you may need to use threading, basically tkinter will run its own thread and without threading your python script would only run as a single thread therefore any delay on background code would cause a delay on your UI unless you invoke threading.. (could be getting a few steps ahead here but it was an issue i run into immediately when trying to use tkinter)
it also uses a canvas for tkinter which lays a grid over our window and allows .grid location rather than .pack
if you wanted to remove a label/button rather than changing its position you can use "label/button name".grid_forget() before an update_idletasks() and your program would not use that element until a grid location is redefined

Code: Select all

from tkinter import *
import threading
import time

master = Tk()
page = 0
timer = 0

canvas = Canvas(width=1280, height=720, bg='black')
canvas.grid(rowspan=5, columnspan=8, sticky='W,E,N,S')


def background_math():
    global timer
    start = time.time()
    while 1:
        now = time.time()
        timer = now - start
        time.sleep(1)


def main_page():
    global page
    page = 0
    block.config(bg='red', text=page)
    block.grid(padx=5, pady=10, row=0, column=0, sticky='W,E,N,S')
    next_page.config(command=page_1)
    time_label.config(text="script run for " + ("%02d" % timer) + " seconds")
    master.update_idletasks()


def page_1():
    global page
    page = 1
    block.config(bg='green', text=page)
    block.grid(padx=5, pady=10, row=1, column=1, sticky='W,E,N,S')
    next_page.config(command=page_2)
    time_label.config(text="script run for " + ("%02d" % timer) + " seconds")
    master.update_idletasks()


def page_2():
    global page
    page = 2
    block.config(bg='pink', text=page)
    block.grid(padx=5, pady=10, row=2, column=2, sticky='W,E,N,S')
    next_page.config(command=page_3)
    time_label.config(text="script run for " + ("%02d" % timer) + " seconds")
    master.update_idletasks()


def page_3():
    global page
    page = 3
    block.config(bg='blue', text=page)
    block.grid(padx=5, pady=10, row=3, column=3, sticky='W,E,N,S')
    next_page.config(command=main_page)
    time_label.config(text="script run for " + ("%02d" % timer) + " seconds")
    master.update_idletasks()


block = Label(master, text=page, width=20, font=('aharoni', 18, 'bold'), bg='red', fg='white')
block.grid(padx=5, pady=10, row=0, column=0, sticky='W,E,N,S')

next_page = Button(master, text="+", width=5, font=('aharoni', 18, 'bold'), bg='orange', fg='white')
next_page.grid(padx=10, pady=10, row=4, column=7, sticky='W,E,N,S')

time_label = Label(master, width=10, font=('aharoni', 18, 'bold'), bg='black', fg='white')
time_label.grid(padx=5, pady=10, row=4, column=0, columnspan=4, sticky='W,E,N,S')

main_page()

t = threading.Thread(target=background_math)
t.daemon = True
t.start()

master.mainloop()

Return to “Python”

Who is online

Users browsing this forum: Idahowalker and 13 guests