Change ttk Progress Bar Background In Tkinter - python

I've been working on a cookie clicker program which includes a progress Bar widget. The progress bar is working perfectly, however when I run my program, the progress bar is surrounded by an ugly grey border.I have tried using highlightbackground but that does nothing. I am working on Mac OSX.
Here's an image of the progress bar when the program is run: https://i.stack.imgur.com/Y7Tue.png
Here is my code:
from Tkinter import *
from Tkinter import Canvas
import ttk
window1 = Tk()
window1.title("Cookie Clicker")
window1.config(background="dodger blue")
window1.geometry("254x370")
clicks = 0
def cookie_clicks():
global clicks
clicks = clicks + 1
pb.step(1)
print("{0}".format(clicks))
if clicks == 1:
lbl1.configure(text="{0} Cookie!".format(clicks))
else:
lbl1.configure(text="{0} Cookies!".format(clicks))
cookie = Button(window1, highlightbackground="dodger blue", borderwidth=0, cursor="hand2", command=cookie_clicks)
photo = PhotoImage(file="imageedit_3_3213999137.gif")
cookie.config(image=photo, width="250", height="250")
cookie.place(x=0, y=90)
w = Canvas(window1, width=254, height=80, highlightbackground="gray")
w.pack()
w.create_rectangle(10, 10, 80, 80, outline="gray", fill="gray", width=100000)
w2 = Canvas(window1, width=0.1, height=250, highlightbackground="dodger blue")
w2.place(x=0, y=85)
w3 = Canvas(window1, width=0.1, height=250, highlightbackground="dodger blue")
w3.place(x=249, y=85)
w4 = Canvas(window1, width=250, height=0.1, highlightbackground="dodger blue")
w4.place(x=0, y=88)
w5 = Canvas(window1, width=250, height=0.1, highlightbackground="dodger blue")
w5.place(x=0, y=338)
lbl1 = Label(window1, bg="gray", fg="dodger blue", text="{0} Cookies!".format(clicks), font=("kavoon", 20))
lbl1.place(x=75, y=40)
lbl2 = Label(window1, bg="gray", fg="dodger blue", text="Cookie Clicker", font=("kavoon", 30))
lbl2.place(x=20, y=0)
pb = ttk.Progressbar(window1, orient='horizontal', mode='determinate')
pb.place(x=76, y=68)
window1.mainloop()
Any help would be appreciated!

Related

Why does opening up another file with Tkinter cause another Tkinter menu to pop up?

In my game, I wanted to have a launcher with some settings on it. These include volume, resolutions, battle speed, etc. When the player is done setting up, they can press the start game button to run the python file that is...well the game.
This is what the configuration menu looks like:
However, after pressing start game, instead of running the game's script, FECD.py, it opens up a blank pygame window and another tkinter menu with a few less options and a default font:
Pressing Start game on the second menu now brings up a syntax error:
return self.func(*args)
File "C:\Users\Kayra Yorulmaz\Desktop\Resume Builders (1)\Time-Based-Strategy-Game-master\Configuration.py", line 95, in OpenServer
exec(open("FECD.py").read())
File "<string>", line 46, in <module>
TypeError: argument 1 must be 2-item sequence, not IntVar
Here's the code for the configuration menu:
import tkinter as tk
import tkinter.font as tkFont
from tkinter import OptionMenu, Scale, HORIZONTAL, colorchooser, Button, BooleanVar, Checkbutton
window = tk.Tk()
window.title("FECD Setup")
window.geometry("320x580")
window.resizable(True, True)
ConfigFont = tkFont.Font(family="Ancient Modern Tales", size=15)
# Settings Label
Settings_lbl = tk.Label(text="Settings", fg="black", font=ConfigFont, compound="center")
Settings_lbl.pack(anchor=tk.CENTER)
v = tk.IntVar()
#Resolution Label
Resolutions_lbl = tk.Label(text="Resolutions", fg="black", width=15, height=1, font=ConfigFont)
Resolutions_lbl.pack(anchor=tk.W)
#Resolution Dropdown
Resolution = tk.IntVar()
Resolution.set((848, 480))
Resolutions_Drpdwn = OptionMenu(window, Resolution, (848, 480), (1280,720), (1920,1080), (2560,1440), (3840,2160))
Resolutions_Drpdwn.pack(anchor=tk.W)
#FullScreen Checkbox
FullScreen_state = BooleanVar()
FullScreen_state.set(True) #set check state
chk = Checkbutton(window, text='Choose', var=FullScreen_state)
#Volume Label
Volume_lbl = tk.Label(text="Volume", fg="black", width=15, height=1, font=ConfigFont)
Volume_lbl.pack(anchor=tk.W)
#Background Music Volume Label
BGMVolume_lbl = tk.Label(text="Background Music Volume", fg="black", width=25, height=1, font=ConfigFont)
BGMVolume_lbl.pack(anchor=tk.W)
#Background Music Volume Slider
BGMVol_Slider = Scale(window, from_=0, to=100, orient=HORIZONTAL, font=ConfigFont)
BGMVol_Slider.set(50)
BGMVol_Slider.pack(anchor=tk.W)
BGVolume = BGMVol_Slider.get()/10
#Sound Effects Volume Label
SFXVolume_lbl = tk.Label(text="Sound Effects Volume", fg="black", width=25, height=1, font=ConfigFont)
SFXVolume_lbl.pack(anchor=tk.W)
#Sound Effects Volume Slider
SFXVol_Slider = Scale(window, from_=0, to=100, orient=HORIZONTAL, font=ConfigFont)
SFXVol_Slider.set(50)
SFXVol_Slider.pack(anchor=tk.W)
Sound_Output = tk.IntVar()
Sound_Output.set(2)
#Mono Sound Radiobutton
Mono_Sound_Rb = tk.Radiobutton(window, text="Mono", fg="black", padx=20, variable=Sound_Output, value=1, font=ConfigFont)
Mono_Sound_Rb.pack(anchor=tk.W)
#Stereo Sound Radiobutton
Stereo_Sound_Rb = tk.Radiobutton(window, text="Stereo", fg="black", padx=20, variable=v, value=2, font=ConfigFont)
Stereo_Sound_Rb.pack(anchor=tk.W)
#Gameplay Settings Label
Gameplay_Settings_lbl = tk.Label(text="Gameplay Options", fg="black", width=15, height=1, font=ConfigFont)
Gameplay_Settings_lbl.pack(anchor=tk.W)
#Color Settings Label
Color_Settings_lbl = tk.Label(text="Menu Box Color", fg="black", width=20, height=1, font=ConfigFont)
Color_Settings_lbl.pack(anchor=tk.W)
#Color Chooser Button
def choose_color():
clr = colorchooser.askcolor(title="Select Color")
print(clr)
Color_Chooser_btn = Button(window, command=choose_color, text="choose color", borderwidth=1, font=ConfigFont)
Color_Chooser_btn.pack(anchor=tk.W)
#Battle Speed Label
Battle_Speed_lbl = tk.Label(text="Battle Speed", fg="black", width=20, height=1, font=ConfigFont)
Battle_Speed_lbl.pack(anchor=tk.W)
#Battle Speed Volume Slider
Battle_SPD_Slider = Scale(window, from_=0, to=200, orient=HORIZONTAL, font=ConfigFont)
Battle_SPD_Slider.set(100)
Battle_SPD_Slider.pack(anchor=tk.W)
BattleSpeed = Battle_SPD_Slider.get()/200
#Game Start Button
def OpenServer():
exec(open("FECD.py").read())
Game_Start_btn = Button(window, text="Start Game", command=OpenServer, borderwidth=2, relief=tk.GROOVE, font=ConfigFont)
Game_Start_btn.pack(anchor=tk.W)
window.mainloop()

Alarm clock not working on my tkinter GUI

Im trying to make a tkinter GUI with an alarm clock and weather. My alarm clock is not working for some reason. I "borrowed" the code from another person and it works on his/her program for some reason but not mine.
I haven't tried anything because im very new to python and coding in general.
import tkinter as tk
from tkinter import *
from tkinter.ttk import *
import os
import time
from time import strftime
from datetime import date
today = date.today()
HEIGHT = 500
WIDTH = 500
def test_function(entry):
label3 = tk.Label(lower_frame, bg='white',font=('Courier',15), text=entry, anchor='nw', justify='left', bd=4)
label3.place(relwidth=1, relheight=1)
root = tk.Tk()
def quit_function():
root.destroy()
def SubmitButton():
AlarmTime= alarm_entry.get()
CurrentTime= time.strftime("%H:%M")
while AlarmTime != CurrentTime:
CurrentTime = time.strftime("%H:%M")
time.sleep(1)
if AlarmTime == CurrentTime:
label3 = tk.Label(lower_frame, bg='white',font=('Courier',15), text="Perkele", anchor='nw', justify='left', bd=4)
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()
background_image = tk.PhotoImage(file='landscape.png')
bg_label = tk.Label(root, image=background_image)
bg_label.place(x=0, y=0, relwidth=1, relheight=1)
frame = tk.Frame(root, bg='#08AEF6', bd=5)
frame.place(relx=0.5, rely=0.125, relwidth=0.75, relheight=0.1, anchor='n')
entry = tk.Entry(frame, font=40)
entry.place(relwidth=0.65, relheight=1)
button = tk.Button(frame, font=40, text="Get Weather", bg='gray', fg='black', command=lambda: test_function(entry.get()))
button.place(relx=0.7, relwidth=0.3, relheight=1)
lower_frame = tk.Frame(root, bg='#08AEF6', bd=10)
lower_frame.place(relx=0.5, rely=0.25, relwidth=0.75, relheight=0.6, anchor='n')
up_frame = tk.Frame(root, bg='#08AEF6', bd=5)
up_frame.place(relx=0.675, rely=0, relwidth=0.4, relheight=0.1, anchor='n')
alarm_button = tk.Button(up_frame, text="Set alarm time", bg='gray', fg='black', font=40, command=SubmitButton)
alarm_button.place(relx=0.7, rely=0.5, relwidth=0.3, relheight=0.45)
label3 = tk.Label(lower_frame, bg='white')
label3.place(relwidth=1, relheight=1)
label = tk.Label(up_frame, bg='white')
label.place(relx=0, relwidth=0.695, relheight=0.5)
date1 = tk.Label(up_frame, bg='white', text=today, font=40)
date1.place(relx=0, rely=0.5, relwidth=0.695, relheight=0.5)
quit_frame = tk.Frame(root, bg='#08AEF6', bd=5)
quit_frame.place(relx=0, rely=0, relwidth=0.1, relheight=0.1)
quit_button = tk.Button(quit_frame, command=quit_function, text="Quit")
quit_button.place(relwidth=1, relheight=1)
alarm_entry = tk.Entry(up_frame, font=40)
alarm_entry.insert(3, "esimerkiksi - 15:45")
alarm_entry.place(relx=0.7, rely=0, relwidth=0.3, relheight=0.45)
# label2 = tk.Label(lower_frame, bg='white')
root.overrideredirect(True)
root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))
root.focus_set()
def time():
string = strftime('%H:%M:%S')
label.config(text = string, font='40')
label.after(1000, time)
time()
It gives me this error in line 33.
AttributeError: 'function' object has no attribute 'strftime'

.after() Causing Tkinter Window To Freeze

I'm making a cookie clicker game in tkinter. I'm trying to program an auto click function for the player to buy that clicks the cookie every ten seconds. I attempted to use the .after() method, however this freezes the program. To clarify, the other functions such as the user clicking the cookie need to still be functional whilst the auto click runs in the background.
Here is the auto click function:
def autoclick():
def tensecs():
while True:
global clicks
clicks = clicks + 1
if clicks == 1:
lbl1.configure(text="{0} Cookie!".format(clicks))
else:
lbl1.configure(text="{0} Cookies!".format(clicks))
shop_window.after(1000, tensecs)
Here is the full program:
from Tkinter import *
from Tkinter import Canvas
import ttk
import time
window1 = Tk()
window1.title("Cookie Clicker")
window1.config(background="dodger blue")
window1.geometry("254x390")
clicks = 0
class FlashableLabel(Label):
def flash(self, count):
bg = self.cget('background')
fg = self.cget('foreground')
self.configure(background=fg, foreground=bg)
count += 1
if count < 1000:
self.after(250, self.flash, count)
def cookie_clicks(event):
global clicks
clicks = clicks + 1
pb.step(10)
print("{0}".format(clicks))
if clicks == 1:
lbl1.configure(text="{0} Cookie!".format(clicks))
else:
lbl1.configure(text="{0} Cookies!".format(clicks))
def about_btn():
about_window = Toplevel(window1)
about_window.overrideredirect(1)
about_window.geometry("230x150+15+50")
about_window.config(background="snow2")
photo4 = PhotoImage(file="imageedit_4_8477195169.gif")
photolbl = Label(about_window, image=photo4, borderwidth=0)
photolbl.image = photo4
photolbl.pack()
lbl3 = Label(about_window, text="Cookie Clicker", bg="snow2", fg="black", font=("Lucida Grande bold", 14))
lbl3.pack()
lbl4 = Label(about_window, text="Version 13.0.4 (24502.5.6)", bg="snow2", fg="black", font=("Lucida Grande", 9))
lbl4.pack()
copyright_symb = u'\N{COPYRIGHT SIGN}'.encode('utf-8')
lbl5 = Label(about_window, text="Copyright {0} 2003-2018 Hatna Inc.\nAll rights reserved.".format(copyright_symb), bg="snow2", fg="black", font=("Lucida Grande", 10))
lbl5.pack()
def shop_btn():
def autoclick():
def tensecs():
while True:
global clicks
clicks = clicks + 1
if clicks == 1:
lbl1.configure(text="{0} Cookie!".format(clicks))
else:
lbl1.configure(text="{0} Cookies!".format(clicks))
shop_window.after(1000, tensecs)
def double():
global clicks
if clicks >= 20:
clicks = clicks * 2
clicks = clicks - 20
if clicks == 1:
lbl1.configure(text="{0} Cookie!".format(clicks))
else:
lbl1.configure(text="{0} Cookies!".format(clicks))
shop_window.destroy()
global lbl3
lbl3.configure(text="", fg="dodger blue")
else:
shop_window.destroy()
lbl3.configure(text="You Need More Cookies!", fg="snow2")
shop_window = Toplevel(window1)
shop_window.overrideredirect(1)
shop_window.geometry("254x310+5+50")
frame = Frame(shop_window, width=254, height=310)
frame.pack()
canvas = Canvas(frame, width=254, height=310, bg="dodger blue", bd=0, highlightthickness=0, relief="ridge", scrollregion=(0, 0, 500, 500))
vbar = Scrollbar(frame, orient=VERTICAL)
vbar.pack(side=RIGHT, fill=Y)
vbar.config(command=canvas.yview)
canvas.config(yscrollcommand=vbar.set)
canvas.pack()
canvas.create_rectangle(0, 0, 254, 40, outline="snow2", fill="snow2")
lbl3 = canvas.create_text(135, 20, text="{0} Cookies".format(clicks), fill="dodger blue", font=("kavoon", 15))
global photo5
photo5 = PhotoImage(file="imageedit_1_6195264074.gif")
photolbl = canvas.create_image(20, 20, image=photo5)
x2 = canvas.create_text(35, 120, text="X2", fill="gray", font=("arial bold", 40))
lbl4 = canvas.create_text(115, 110, text="Double Your Score", fill="black", font=("arial bold", 12))
lbl5 = canvas.create_text(94, 129, text="20 Cookies", fill="black", font=("arial", 12))
buybtn1 = Button(canvas, text="Buy", highlightbackground="dodger blue", command=double)
buybtn1_window = canvas.create_window(210, 119.5, window=buybtn1)
global photo6
photo6 = PhotoImage(file="imageedit_24_7373388957.gif")
photolbl2 = canvas.create_image(32.5, 70, image=photo6)
lbl6 = canvas.create_text(124, 61, text="10 Second Auto-Click", fill="black", font=("arial bold", 12))
lbl5 = canvas.create_text(94, 80, text="15 Cookies", fill="black", font=("arial", 12))
buybtn2 = Button(canvas, text="Buy", highlightbackground="dodger blue", command=autoclick)
buybtn2_window = canvas.create_window(210, 70, window=buybtn2)
w = Canvas(window1, width=254, height=80, highlightbackground="snow2")
w.pack()
w.create_rectangle(10, 10, 80, 80, outline="snow2", fill="snow2", width=100000)
cookie = Button(window1, highlightbackground="dodger blue", borderwidth=0, cursor="hand2")
cookie.bind('<Button-1>', cookie_clicks)
window1.bind('<Return>', cookie_clicks)
photo = PhotoImage(file="imageedit_3_3213999137.gif")
cookie.config(image=photo, width="250", height="250")
cookie.place(x=0, y=90)
w2 = Canvas(window1, width=0.1, height=250, highlightbackground="dodger blue")
w2.place(x=0, y=86)
w3 = Canvas(window1, width=0.1, height=250, highlightbackground="dodger blue")
w3.place(x=249, y=86)
w4 = Canvas(window1, width=250, height=0.1, highlightbackground="dodger blue")
w4.place(x=0, y=88)
w5 = Canvas(window1, width=250, height=0.1, highlightbackground="dodger blue")
w5.place(x=0, y=338)
lbl1 = Label(window1, bg="snow2", fg="dodger blue", text="{0} Cookies!".format(clicks), font=("kavoon", 20))
lbl1.place(x=75, y=40)
lbl2 = Label(window1, bg="snow2", fg="dodger blue", text="Cookie Clicker", font=("kavoon", 30))
lbl2.place(x=20, y=0)
pb = ttk.Progressbar(window1, orient='horizontal', mode='determinate')
pb.place(x=76, y=68)
shop = Button(window1, highlightbackground="dodger blue", borderwidth=0, command=shop_btn)
photo2 = PhotoImage(file="imageedit_28_9392607524.gif")
shop.config(image=photo2, width="44", height="40")
shop.place(x=5, y=345)
w6 = Canvas(window1, width=38, height=0.1, highlightbackground="dodger blue")
w6.place(x=7, y=345)
w7 = Canvas(window1, width=38, height=0.1, highlightbackground="dodger blue")
w7.place(x=7, y=381)
w8 = Canvas(window1, width=0.1, height=25, highlightbackground="dodger blue")
w8.place(x=6, y=350)
w9 = Canvas(window1, width=0.1, height=25, highlightbackground="dodger blue")
w9.place(x=45, y=350)
info = Button(window1, highlightbackground="dodger blue", borderwidth=0, command=about_btn)
photo3 = PhotoImage(file="imageedit_48_3021448243.gif")
info.config(image=photo3, width="44", height="40")
info.place(x=49, y=345)
w10 = Canvas(window1, width=38, height=0.1, highlightbackground="dodger blue")
w10.place(x=51, y=345)
w11 = Canvas(window1, width=38, height=0.1, highlightbackground="dodger blue")
w11.place(x=51, y=381)
w12 = Canvas(window1, width=0.1, height=25, highlightbackground="dodger blue")
w12.place(x=50, y=350)
w13 = Canvas(window1, width=0.1, height=25, highlightbackground="dodger blue")
w13.place(x=90, y=350)
lbl3 = FlashableLabel(window1, bg="dodger blue", fg="dodger blue", text="", font=("kavoon", 12))
lbl3.place(x=100, y=353)
lbl3.flash(0)
window1.mainloop()
Any help would be appreciated! and please excuse my GUI's bad formatting, I've not quite began to experiment with .grid.
You need to use after() instead of a while loop, not in addition to. Try this:
def autoclick():
global clicks
clicks = clicks + 1
if clicks == 1:
lbl1.configure(text="{0} Cookie!".format(clicks))
else:
lbl1.configure(text="{0} Cookies!".format(clicks))
shop_window.after(1000, autoclick) # repeat this function every second

Tkinter Link Button With Progress Bar

I'm creating a cookie clicker app using Tkinter. I want to have a progress bar in my program that fills up every time the cookie is clicked 100 times. I have created the progress bar using import ttk, however I don't know how to make the progress bar update whenever the button is pressed.
Here is my code so far:
from Tkinter import *
from Tkinter import Canvas
import ttk
window1 = Tk()
window1.title("Cookie Clicker")
window1.config(background="dodger blue")
window1.geometry("254x370")
clicks = 0
def cookie_clicks():
global clicks
clicks = clicks + 1
print("{0}".format(clicks))
if clicks == 1:
lbl1.configure(text="{0} Cookie!".format(clicks))
else:
lbl1.configure(text="{0} Cookies!".format(clicks))
cookie = Button(window1, highlightbackground="dodger blue", borderwidth=0, cursor="hand2", command=cookie_clicks)
photo = PhotoImage(file="imageedit_3_3213999137.gif")
cookie.config(image=photo, width="250", height="250")
cookie.place(x=0, y=90)
w = Canvas(window1, width=254, height=75, highlightbackground="gray")
w.pack()
w.create_rectangle(10, 10, 80, 80, outline="gray", fill="gray", width=100000)
w2 = Canvas(window1, width=0.1, height=250, highlightbackground="dodger blue")
w2.place(x=0, y=85)
w3 = Canvas(window1, width=0.1, height=250, highlightbackground="dodger blue")
w3.place(x=249, y=85)
w4 = Canvas(window1, width=250, height=0.1, highlightbackground="dodger blue")
w4.place(x=0, y=88)
w5 = Canvas(window1, width=250, height=0.1, highlightbackground="dodger blue")
w5.place(x=0, y=338)
lbl1 = Label(window1, bg="gray", fg="dodger blue", text="{0} Cookies!".format(clicks), font=("kavoon", 20))
lbl1.place(x=75, y=45)
lbl2 = Label(window1, bg="gray", fg="dodger blue", text="Cookie Clicker", font=("kavoon", 30))
lbl2.place(x=20, y=0)
pb = ttk.Progressbar(window1, orient='horizontal', mode='determinate')
pb.pack()
window1.mainloop()
Any help would be appreciated!
Without changing your code much, you can do:
def cookie_clicks():
global clicks, pb
clicks = clicks + 1
pb.step(1)
print("{0}".format(clicks))
if clicks == 1:
lbl1.configure(text="{0} Cookie!".format(clicks))
else:
lbl1.configure(text="{0} Cookies!".format(clicks))
It is not great to use global variables. You should look into encapsulating your app in a class.

Why is my code printing ".!canvas.!entry"?

I don't understand what is going on. I have defined 2 functions. 1 function displays text, entry boxes, and a submit button. I have it so that when the submit button is pressed, it prints the information the user put inside of the entry boxes. I've shortened my code for easier readability.
from tkinter import *
from PIL import Image, ImageTk
canvas_width = 360
canvas_height = 525
file = r"C:\Users\kraak\Desktop\PyCharm Community Edition 2017.1.2\borderedpaper.GIF"
master = Tk()
canvas = Canvas(master, width=canvas_width, height=canvas_height)
old_img = PhotoImage(file=file)
new_img = old_img.subsample(3, 3)
canvas.create_image(-11, -10, anchor=NW, image=new_img)
canvas.create_window(0, 0, height=1, width=1, anchor=NW)
canvas.create_text(0, 0, text="Test")
e1 = Entry(canvas)
def callback():
print(e1)
def answer():
e1 = Entry(canvas)
canvas.create_window(250, 100, window=e1, height=15, width=100)
label = Label(text="Enter a word.")
label.place(x=40, y=90)
e2 = Entry(canvas)
canvas.create_window(250, 125, window=e2, height=15, width=100)
label = Label(text="Enter a word.")
label.place(x=40, y=115)
button = Button(text="Submit.", command=callback)
button.place(x=150, y=460)
answer()
canvas.pack()
mainloop()

Categories