I think I made a mistake with this tkinter widget - python

So till now I want to make a simple button but it gives me an error screen, what am I doing wrong? Here's my code:
import tkinter as tk
import math
import time
tk = tk.Tk()
tk.geometry()
tk.attributes("-fullscreen", True)
exit_button = tk.Button(tk, text = "Exit", height = 2, width = 2, command = tk.destroy)
exit_button.place(x=1506, y=0)
tk.mainloop()

You are shadowing tk with something else:
import tkinter as tk
root = tk.Tk()
root.geometry()
root.attributes("-fullscreen", True)
exit_button = tk.Button(root, text="Exit", height=2, width=2, command=root.destroy)
exit_button.place(x=1506, y=0)
tk.mainloop()

You cannot use tk = tk.Tk(), because you are also referring to tkinter as tk. So either:
Change your imports(not recommended):
import tkinter as _tk
tk = _tk.Tk() # And so on..
or change your variable name(recommended):
root = tk.Tk() # And change tk.geometry to root.geometry() and so on

Related

Python: How to use tkinter to open various scripts (bokeh)?

I have a bokeh code that I open it with the terminal typing:
bokeh serve --show graph2d.py
how can I make a tkinter open this bokeh.py(graph2d.py)?
Please help with this, I dont find how to make it work.
this is the tkinter:
import tkinter as tk
from tkinter import *
import graph2d
"""import tkinter as Tk
from tkinter import ttk"""
window = Tk()
window.title("Welcome to PokerScatter app")
"""
WINDOW_SIZE = "600x400"
root = tk.Tk()
root.geometry(WINDOW_SIZE)
"""
selected = IntVar()
rad1 = Radiobutton(window,text='2d Scatter', value=1, variable=selected, command=graph2d)
rad2 = Radiobutton(window,text='3d Scatter', value=2, variable=selected)
rad3 = Radiobutton(window,text='Pie Chart', value=3, variable=selected)
rad4 = Radiobutton(window,text='Histogram', value=4, variable=selected)
def clicked():
print("Processing")
btn = Button(window, text="Show", command=clicked)
rad1.grid(column=0, row=0)
rad2.grid(column=2, row=0)
rad3.grid(column=0, row=1)
rad4.grid(column=2, row=1)
btn.grid(column=6, row=0)
window.mainloop()

tkinter scale slider value not showing

below is my code for a basic tkinter scale:
root = tk.Tk()
root.geometry('150x75')
root.resizable(width=False, height=False)
root.eval('tk::PlaceWindow . center')
v1 = tk.IntVar()
ttk.Scale(root, from_=0, to=100, orient='horizontal', variable=v1).pack(pady=20)
root.mainloop()
below is the attached output :
why is the slider value not showing above the slider ?
I would very much appreciate if someone could point out the error with the code.
The tkinter.ttk version of Scale does not support showing the value.
But the tkinter version of Scale DOES support showing the Scale value, by default. The showvalue option defaults to 1. You can set it to 0 to hide the value if so desired.
There is no error in your code. I think the answer is "because it's not designed to". There is no mention in the documentation of the ttk scale widget that it shows the value.
Try something like this:-
import tkinter as tk
from tkinter import ttk
# root window
root = tk.Tk()
root.geometry('300x200')
root.resizable(False, False)
root.title('Slider Demo')
root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=3)
# slider current value
current_value = tk.DoubleVar()
def get_current_value():
return '{: .2f}'.format(current_value.get())
def slider_changed(event):
value_label.configure(text=get_current_value())
# label for the slider
slider_label = ttk.Label(root, text='Slider:')
slider_label.grid(column=0, row=0, sticky='w')
# slider
slider = ttk.Scale(root, from_=0, to=100, orient='horizontal', command=slider_changed, variable=current_value)
slider.grid(column=1, row=0, sticky='we')
# current value label
current_value_label = ttk.Label(root, text='Current Value:')
current_value_label.grid(row=1, columnspan=2, sticky='n', ipadx=10, ipady=10)
# value label
value_label = ttk.Label(root,text=get_current_value())
value_label.grid(row=2, columnspan=2, sticky='n')
root.mainloop()
Link to site from which I got the answer:- https://www.pythontutorial.net/tkinter/tkinter-slider/
You may try removing this from tkinter ttk import * if you have imported something like this at the top of your code. Simply importing this from tkinter import * helps in showing the values of the scale in slider.
Happy Coding!
A simple like this:
import tkinter as tk
from tkinter import ttk
from tkinter import *
root = tk.Tk()
root.geometry('150x75')
root.resizable(width=False, height=False)
root.eval('tk::PlaceWindow . center')
def sel():
selection = f"Value = str{var.get()}"
label.config(text = selection)
var = tk.DoubleVar()
scale = tk.Scale(root, variable=var )
scale.pack(anchor=CENTER)
label = Label(root)
label.pack()
root.mainloop()

How can i open a .py file using if and open

import tkinter
from tkinter import ttk
from tkinter import *
window = tkinter.Tk()
window.title("wasans")
window.geometry('640x400')
window.resizable(False, False)
textbook = ['국어','영어','social']
textbookselecter = ttk.Combobox(window, text="교과서 선택", values=textbook)
textbookselecter.grid(column=0, row=0)
textbookselecter.place(x=140, y=200)
textbookselecter.set("목록 선택")
textbooksuntack = ttk.Label(window, width=11, borderwidth=2, background="#8182b8")
textbooksuntack.grid(column=0, row=0)
textbooksuntack.place(x=170, y=170)
textbooksuntack.anchor = CENTER
naeyong = textbookselecter.get()
if naeyong == "social":
open("../social.py", "w")
window.mainloop()
How can I open a py file using if statement and open?
Also, please give your overall review of the code.
I am not good at English, so there may be awkward sentences using a translator. Please understand.
You basically need to check every time the ttk.Combobox is changed to check if 'social' is generated.
ttk.Combobox raises a "<<ComboboxSelected>>" virtual event when selected.
import tkinter
from tkinter import ttk
from tkinter import *
window = tkinter.Tk()
window.title("wasans")
window.geometry('640x400')
window.resizable(False, False)
def callback(eventObject):
if textbookselecter.get() == "social":
print("Opening File...")
#file=open("...../social.py","r+")
textbook = ['국어','영어','social']
textbookselecter = ttk.Combobox(window, text="교과서 선택", values=textbook)
textbookselecter.grid(column=0, row=0)
textbookselecter.place(x=140, y=200)
textbookselecter.set("목록 선택")
textbooksuntack = ttk.Label(window, width=11, borderwidth=2, background="#8182b8")
textbooksuntack.grid(column=0, row=0)
textbooksuntack.place(x=170, y=170)
textbooksuntack.anchor = CENTER
textbookselecter.bind("<<ComboboxSelected>>",callback)
window.mainloop()

How to expand entry box with window in tkinter?

I'm just creating simple window using tkinter which have entry box and search button. What is want is when i maximize window search bar also starch but it is not happening,
Here is my code
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry("500x500")
root.title("Wikipedia")
class first:
def labels(self):
search_frame = ttk.LabelFrame(root)
search_frame.pack(side = "top",fill = "both")
search_var = tk.StringVar()
search_bar = tk.Entry(search_frame,width = 40,textvariable = search_var)
search_bar.grid(row = 0,column = 0)
search_button = ttk.Button(search_frame,text = "Search")
search_button.grid(row = 1,column = 0)
boot = first()
boot.labels()
root.mainloop()
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry("500x500")
root.title("Wikipedia")
class first:
def labels(self):
search_frame = ttk.LabelFrame(root)
search_frame.pack(side="top", fill="both")
search_var = tk.StringVar()
search_bar = tk.Entry(search_frame, width=20, textvariable=search_var)
search_bar.pack(fill="x")
search_button = ttk.Button(search_frame, text="Search")
search_button.pack()
boot = first()
boot.labels()
root.mainloop()
Not sure this is what you're looking for but try it out.
Here I have used fill='x' in the .pack() geometry manager to fill the row

Why are Menubutton not working in this code?

Found a rather interesting way to create menus in the Tkinter GUI - Menubutton. But unfortunately this code does not work (or rather, when you click on Menubutton the bound Menu does not open):
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.option_add("*Menu.borderWidth", "0")
root.option_add("*Menu.activeBorderWidth", "0")
root.option_add("*Menu.background", "black")
style = ttk.Style(root)
menu = tk.Menu(root)
btn_menu = ttk.Menubutton(root, text='fegvd')
btn_menu.pack()
file = tk.Menu(btn_menu, tearoff=0, foreground='white')
file.add_command(label='ГЫГ')
style.configure('TMenubutton', background='black', foreground='white', indicatoron=0, menu=file, direction='delow', state='active')
root.mainloop()
Although, if I use not ttk.Menubutton, but tk.Menubutton, then everything works:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.option_add("*Menu.borderWidth", "0")
root.option_add("*Menu.activeBorderWidth", "0")
root.option_add("*Menu.background", "black")
menu = tk.Menu(root)
btn_menu = tk.Menubutton(root, text='fegvd')
btn_menu.pack()
file = tk.Menu(btn_menu, tearoff=0, foreground='white')
file.add_command(label='ГЫГ')
btn_menu.configure(background='black', foreground='white', indicator=0, menu=file, state='active')
root.mainloop()
Why? Tell me, please, what is the problem?
You cannot use the style to associate the menu with the menubutton. You need to do it exactly like you do with the tk menu:
btn_menu.configure(menu=file)

Categories