Buttons and other active parts of TKinter - python

I have a problem with TKinter radiobuttons, checkerboxes, buttons and fields. I'm not sure if I can put multiple questions in one but basically it's a single question but quite big. First part is that I can't setup my checkerboxes correctly. I wrote a gray bg and almost white fg but on Mac it shows everything in white. I'll attach a few screenshots but I think I described my problem really well. Here is a part of the code with a problem:
buttonVR = tk.Button(window, bg="gray15", text=f"{modes[0]}", font=("Arial", 10), activebackground="black", fg="lemon chiffon", command=VR, width=24, height=3)
import tkinter.messagebox as tmb
import tkinter.filedialog as tfd
from tkinter.ttk import Radiobutton
from tkinter import *
from tkinter.ttk import Checkbutton
import tkinter as tk
import re
import math
global window
def MainWindow():
def HelpMain():
tmb.showinfo(f"Help", "blah-blah-blah, demo v")
window = tk.Tk()
window.title(f"{app_name} {version} by {company}")
window.geometry("500x240")
window.configure(bg="gray22", border= "10")
window.resizable(False, False)
buttonVR = tk.Button(window, bg="gray15", text=f"{modes[0]}", font=(
"Arial", 10,), activebackground="black", fg="lemon chiffon", command=VR, width=24, height=3)
buttonVR.grid(column=0, row=0, pady=5)
buttonPR = tk.Button(window, bg="gray15", text=f"{modes[1]}", font=(
"Arial", 10), activebackground="black", fg="lemon chiffon", command=PR, width=24, height=3)
buttonPR.grid(column=0, row=1, pady=10)
buttonAR = tk.Button(window, bg="gray15", text=f"{modes[2]}", font=(
"Arial", 10), activebackground="black", fg="lemon chiffon", command=AR, width=24, height=3)
buttonAR.grid(column=0, row=2, pady=5)
buttonHelp = tk.Button(window, bg="gray15", text="Help", font=(
"Arial", 10), activebackground="black", fg="lemon chiffon", command=HelpMain, width=4, height=1)
buttonHelp.place(anchor='se', x=480, y=220)
window.mainloop()
Second problem is that I can't take information from a checkerbox. I need to take true/false to activate/deactivate a text field here is a part with a checkerbox itself:
height_chk_state = BooleanVar()
height_chk_state.set(False)
height_chk = tk.Checkbutton(windowVR, border= "5", bg="gray22", selectcolor="gray15", fg="lemon chiffon", text='Height', var=height_chk_state)
height_chk.grid(column=0, row=3)
And here is the full code of a window with checkerboxes:
import tkinter as tk
from tkinter import Radiobutton
from tkinter import *
import tkinter.messagebox as tmb
import tkinter.filedialog as tfd
from tkinter.ttk import Checkbutton
global windowVR
def HelpVR():
tmb.showinfo(f"Help",
"blah-blah-blah")
windowVR = tk.Tk()
windowVR.resizable(False, False)
windowVR.geometry("400x150")
windowVR.title(f"test")
windowVR.configure(bg="gray22", borderwidth=10)
width_chk_state = BooleanVar()
width_chk_state.set(False)
width_chk = tk.Checkbutton(windowVR, border= "5", bg="gray22", fg="lemon chiffon", selectcolor="gray15", text='Width ', var=width_chk_state)
width_chk.grid(column=0, row=1)
height_chk_state = BooleanVar()
height_chk_state.set(False)
height_chk = tk.Checkbutton(windowVR, border= "5", bg="gray22", selectcolor="gray15", fg="lemon chiffon", text='Height', var=height_chk_state)
height_chk.grid(column=0, row=3)
ratio_chk_state = BooleanVar()
ratio_chk_state.set(False)
ratio_chk = tk.Checkbutton(windowVR, border= "5", bg="gray22", fg="lemon chiffon", selectcolor="gray15", text='Ratio ', var=ratio_chk_state)
ratio_chk.grid(column=0, row=5)
global width_condition
global height_condition
global ratio_condition
width_condition = "readonly"
height_condition="readonly"
ratio_condition="readonly"
while 1>0:
if width_chk_state is True:
width_condition="normal"
print(width_condition)
else:
width_condition = "readonly"
print(width_condition)
if height_chk_state is True:
height_condition="normal"
print(height_condition)
else:
height_condition="readonly"
print(height_condition)
if ratio_chk_state is True:
ratio_condition="normal"
print(height_condition)
else:
ratio_condition="readonly"
print(ratio_condition)
Width = tk.Entry(windowVR, width=30, font=(
"Arial", 10), bg="gray12", fg="lemon chiffon", state=f"{width_condition}")
Width.grid(column=5, row=1)
Height = tk.Entry(windowVR, width=30, font=(
"Arial", 10), bg="gray12", fg="lemon chiffon", state=f"{height_condition}")
Height.grid(column=5, row=3)
Ratio = tk.Entry(windowVR, width=30, font=(
"Arial", 10), bg="gray12", fg="lemon chiffon", state=f"{ratio_condition}")
Ratio.grid(column=5, row=5)
print(width_condition)
print(height_condition)
print(ratio_condition)
windowVR.mainloop()
I know that windows looks ugly at the moment but it's just a beginning of a demo so please be condescending to my code:) In terms of colors I tried almost every fitting parameter in buttons and stuff. I solved a problem with checkerboxes by changing the 'selectcolor' to black but it is not working with other types of objects. I tried to create a while cycle to check checkerboxes' conditions but either it falls to an infinite loop without any possibility to go further through code or I just can't refresh it without any stops.
Image 1
Image 2

Related

Tkinter Text widget scroll bar doesn't show the first character

I am trying to make a horizontal scroll bar for a text widget in tkinter, it works but when the text is long, it starts not showing some parts of the first character untill it's totally disappeared.
You can see some pixels of the first character in this image
That's my code:
scrollbar = Scrollbar(window, orient='horizontal')
scrollbar.pack(side=BOTTOM, fill=X)
text = Text(window, font=("Calibri", 40), xscrollcommand=scrollbar.set)
text.tag_configure("center", justify='center')
text.insert("1.0", "222222222222222222222222222222")
text.tag_add("center", "1.0", "end")
text.config(width=100, height=1, background="#f2f2f2", borderwidth=0, state='disabled', wrap='none')
text.pack(pady=24)
scrollbar.config(command=text.xview)
I changed root.resizable(280, 20), try this:
from tkinter import*
root = Tk()
root.resizable(280, 20)
root.title("Scrollbar Widget Example")
scrollbar = Scrollbar(root, orient='horizontal')
scrollbar.pack(side=BOTTOM, fill=X)
text = Text(root, font=("Calibri", 40), xscrollcommand=scrollbar.set)
text.tag_configure("center", justify='center')
text.insert("1.0", "222222222222222222222222222222")
text.tag_add("center", "1.0", "end")
text.config(width=30, height=1, background="#f2f2f2", borderwidth=0, state='disabled', wrap='none')
scrollbar.config(command=text.xview)
text.pack(pady=24)
root.mainloop()

How to add a scrollbar to a Text in tkinter?

I have made a code in which you type a question and it fetches an answer from Wikipedia once you press the 'Search' button. However decent I may be at python and tkinter, I'm stuck on this. I know how to add a scrollbar, set it, set it's command, set the y/xscrollcommand and pack it but it's not working and I could use some help.
Thanks in advance for any help I get.
#-----Import library-----#
import wikipedia
from tkinter import *
#-----Create window-----#
window=Tk()
iconpic=PhotoImage(file='Cat.png')
window.iconphoto(False, iconpic)
window.title('ChatCat')
window.geometry('400x517')
window.config(bg='#242121')
window.resizable(width=FALSE, height=FALSE)
#-----Define functions-----#
def two_sentence():
global number_o_sentences
number_o_sentences=2
def three_sentence():
global number_o_sentences
number_o_sentences=3
def four_sentence():
global number_o_sentences
number_o_sentences=4
def five_sentence():
global number_o_sentences
number_o_sentences=5
def reply():
Label(chatWindow, text=(messageWindow.get('1.0', 'end-1c')), wraplength='250', justify='right', fg='#502ec9', bg='#242121').pack(side='top', anchor='e', pady=3)
Label(chatWindow, text=(wikipedia.summary(messageWindow.get('1.0', 'end-1c'), sentences=number_o_sentences)), wraplength='350', justify='left', fg='#b02832', bg='#242121').pack(side='top', anchor='w', pady=3)
#-----Create menu-----#
main_menu=Menu(window)
main_menu.add_command(label="Two", command=two_sentence)
main_menu.add_command(label="Three", command=three_sentence)
main_menu.add_command(label="Four", command=four_sentence)
main_menu.add_command(label="Five", command=five_sentence)
main_menu.add_command(label="Quit", command=window.quit)
window.config(menu=main_menu)
#-----Create conversation window-----#
scrollbar = Scrollbar(window, cursor="target")
scrollbar.pack(side=RIGHT, fill='y')
chatWindow = Text(window, bd=1, bg="black", width="50", height="8", font=("Arial", 23), foreground="#00ffff", yscrollcommand=scrollbar.set)
chatWindow.place(x=6,y=6, height=385, width=370)
messageWindow = Text(window, bd=0, bg="black",width="30", height="4", font=("Arial", 23), foreground="#00ffff", highlightcolor='white', borderwidth=2)
messageWindow.place(x=128, y=400, height=88, width=247)
scrollbar.config(command=chatWindow.yview)
button=Button(window, text="Search", width="12", height=5, bd=0, bg="#2d3652", activebackground="#00bfff",foreground='#ffffff',font=("Arial", 12), command=reply)
button.place(x=6, y=400, height=88)
Label(chatWindow, text='Select the number of sentences you want above and start searching', wraplength='350', justify='left', fg='#b02832', bg='#242121').pack(side='top', anchor='w', pady=3)
#-----Run window-----#
window.mainloop()

Program not running when double clicked but runs from cmd?

I have made a python gui using tkinter and i have been running it through the commandline. But when i tried to open it by double clicking, it shortly shows the window but exits again. I tried commenting out all the places where it uses images thinking that it is because it dosent have file-editing permissions when its run by double click and it worked! But i need to use images in the gui, anyone have solutions?
import tkinter as tk
from PIL import Image, ImageTk
import time
root = tk.Tk()
root.title("Krypto Coin")
root.iconbitmap('icon.ico')
root.configure(bg="#112233")
Frame = tk.Frame(root, width=1200, height=800, bg="#112233")
Frame.grid(columnspan=3, rowspan=5)
icon = Image.open('icon_round.png')
icon = ImageTk.PhotoImage(icon)
icon_label = tk.Label(image=icon, borderwidth=0, highlightthickness=0)
icon_label.place(relx=0.5, rely=0.2, anchor="center")
text = tk.Label(root, text="Welcome to Krypto Coin (KTC)", bg="#112233", fg="#ffffff", font=("Arial", 32))
text.place(relx=0.5, rely=0.45, anchor="center")
text = tk.Label(root, text="Enter id of the receiver and amount", bg="#112233", fg="#ffffff", font=("Arial", 24))
text.place(relx=0.5, rely=0.5, anchor="center")
def trasfer_button_pressed():
time.sleep(0.1)
trasfer_button_text.set("Loading...")
trasfer_button.configure(state="disable")
trasfer_button.configure(bg="#112233")
input_id.configure(state="disable")
input_amount.configure(state="disable")
def trasfer_button_enter(e):
if trasfer_button_text.get() == "Transfer":
trasfer_button.configure(bg="#334455")
def trasfer_button_leave(e):
if trasfer_button_text.get() == "Transfer":
trasfer_button.configure(bg="#223344")
trasfer_button_text = tk.StringVar()
trasfer_button = tk.Button(root, textvariable=trasfer_button_text, command=lambda:trasfer_button_pressed(), bg="#223344", fg="#ffffff", width=15, height=1, font=("Arial", 24), borderwidth=0, highlightthickness=0)
trasfer_button_text.set("Transfer")
trasfer_button.place(relx=0.5, rely=0.65, anchor="center")
trasfer_button.bind("<Enter>", trasfer_button_enter)
trasfer_button.bind("<Leave>", trasfer_button_leave)
input_id = tk.Entry(root, width=48, font = "Arial 14", bg="#334455", fg="#ffffff", borderwidth=0, highlightthickness=0)
input_id.insert(0, "ID")
input_id.place(relx=0.45, rely=0.8, anchor="center", height=30)
input_amount = tk.Entry(root, width=20, font = "Arial 14", bg="#334455", fg="#ffffff", borderwidth=0, highlightthickness=0)
input_amount.insert(0, "Amount")
input_amount.place(relx=0.65, rely=0.8, anchor="center", height=30)
root.mainloop()

How do I use grid_forget more than once in tkinter?

I'm trying to give myself Excercises using imports and keeping programs and modules separate from each other. In this particular excercise I've given myself, I have a basic feet-to-inches and inches-to-feet converter.
Everything works except for one problem. Where the answer is displayed on the GUI, a new answer covers the old answer, rather than replace.
import tkinter as tk
from tkinter import E, W
import Converter
window = tk.Tk()
window.title("Converter")
answer_label = tk.Label(window, text="No Answer")
answer_label.grid(row=5)
def feet_to_inches_function(old_label):
old_label.grid_forget()
answer_label = tk.Label(window, text=Converter.feet_to_inches(int(entry_bar.get())))
answer_label.grid(row=5)
def inches_to_feet_function(old_label):
old_label.grid_forget()
answer = tk.Label(window, text=Converter.inches_to_feet(int(entry_bar.get())))
answer.grid(row=5)
title_label = tk.Label(window, text="Convert")
entry_bar = tk.Entry(window, font=('HELVETICA', 10))
fti_button = tk.Button(window, text="Feet to Inches", command=lambda: feet_to_inches_function(answer_label))
itf_button = tk.Button(window, text="Inches to Feet", command=lambda: inches_to_feet_function(answer_label))
quit_button = tk.Button(window, text="Quit", command=window.destroy)
title_label.grid(row=0, column=0, sticky=W)
entry_bar.grid(row=1, columnspan=2, sticky=(E, W))
fti_button.grid(row=3, column=0)
itf_button.grid(row=3, column=1)
quit_button.grid(row=4, columnspan=2, sticky=(E, W))
window.mainloop()
Also as a separate question,I'm having a hard time understanding mainloop.I know that it makes the program go in an infinate loop, but from where? I don't see any logical evidence that shows that my code is on an endless loop.
Thanks so much
Here's a solution for you:
import tkinter as tk
from tkinter import E, W
window = tk.Tk()
window.title("Converter")
answer_label = tk.Label(window, text="No Answer")
answer_label.grid(row=5)
def feet_to_inches_function():
answer_label.configure(text='As you want (INCHES)')
def inches_to_feet_function():
answer_label.configure(text='As you want (FEET)')
title_label = tk.Label(window, text="Convert")
entry_bar = tk.Entry(window, font=('HELVETICA', 10))
fti_button = tk.Button(window, text="Feet to Inches", command=feet_to_inches_function)
itf_button = tk.Button(window, text="Inches to Feet", command=inches_to_feet_function)
quit_button = tk.Button(window, text="Quit", command=window.destroy)
title_label.grid(row=0, column=0, sticky=W)
entry_bar.grid(row=1, columnspan=2, sticky=(E, W))
fti_button.grid(row=3, column=0)
itf_button.grid(row=3, column=1)
quit_button.grid(row=4, columnspan=2, sticky=(E, W))
window.mainloop()
All you need is to configure the text of the Label in both functions.

Changing part of a message's color in tkinter messagebox

I have a TKinter messagebox like the one below. I would like to change part of the message to a different color. For example in the messagebox below I would like the language to be Blue. Is this possible?
It's not possible to change such options of Tkinter Standard Dialogs. You need to create your own dialog. You'll also need to separate the text parts. I've tried to make something similar in the image that the OP has posted above:
from tkinter import *
root = Tk()
def choosefunc(option):
if option == "cancel":
print("Cancel choosen")
else:
print("OK choosen")
def popupfunc():
tl = Toplevel(root)
tl.title("Languages")
frame = Frame(tl)
frame.grid()
canvas = Canvas(frame, width=100, height=130)
canvas.grid(row=1, column=0)
imgvar = PhotoImage(file="pyrocket.png")
canvas.create_image(50,70, image=imgvar)
canvas.image = imgvar
msgbody1 = Label(frame, text="The", font=("Times New Roman", 20, "bold"))
msgbody1.grid(row=1, column=1, sticky=N)
lang = Label(frame, text="language(s)", font=("Times New Roman", 20, "bold"), fg='blue')
lang.grid(row=1, column=2, sticky=N)
msgbody2 = Label(frame, text="of this country is: Arabic", font=("Times New Roman", 20, "bold"))
msgbody2.grid(row=1, column=3, sticky=N)
cancelbttn = Button(frame, text="Cancel", command=lambda: choosefunc("cancel"), width=10)
cancelbttn.grid(row=2, column=3)
okbttn = Button(frame, text="OK", command=lambda: choosefunc("ok"), width=10)
okbttn.grid(row=2, column=4)
label = Label(root, text="Click to proceed:")
label.grid()
button = Button(root, text="Click", command=popupfunc)
button.grid()
(Image URL: http://imgur.com/a/Nf75v)

Categories

Resources