I've created a cascade item backbutton under file in self.init_window() yet when I try to change the command of this button in self.writeWindow() I get the error'NoneType' object has no attribute 'configure'`. What am i doing wrong? thanks
#import tkinter libs from python
from tkinter import *
import os
#main class
class Window(Frame):
#__init__ = initilise (run straigh away)
def __init__(self, master = None):
Frame.__init__(self, master)
self.master = master
self.init_window()
def init_window(self):
#define global variables
global backbutton
#main window title
self.master.title("")
self.pack(fill=BOTH, expand=1)
#create a menu
menu = Menu(self.master)
self.master.config(menu=menu)
#create file icon on casacde
file=Menu(menu)
#add dropdown items
menu.add_cascade(label="File", menu=file)
#create help icon on cascade
backbutton = file.add_command(label="Back", command = self.backCommand)
#creating buttons
#the read buttons calls the method readCommand
buttonwrite = Button(self, text="Write File", command=self.writeWindow)
#placing the buttons in place on the GUI
buttonwrite.grid(row=2, column=1)
#adding a label
question = Label(self, text="Please Select an Option: ", font=("Helvetica", 16))
question.grid(row=1, column=1)
def writeWindow(self):
#This is not working -- change the command of the backbutton dropdown
backbutton.configure(0, command = self.backCommand1)
#Changing the name of the window, to become releveant
self.master.title("Write Files")
self.pack(fill=BOTH, expand=1)
def backCommand(self):
print("")
def backCommand1(self):
print("")
#base geometry for the main window
root = Tk()
root.geometry("400x100")
app = Window(root)
root.mainloop()
add_command doesn't return an object that can be configured. If you want to reconfigure a menu item you must use entryconfigure and you must call it on the menu, giving it the index of an item to configure. The index can be a number or the item label (or a few other things).
For example:
self.fileMenu = Menu(...)
self.fileMenu.add_command(label="Back", ...)
...
self.fileMenu.entryconfigure("Back", command=...)
Related
My aim is to generate a window with a button "NewWindow" on it. If I press the button, the program should open a new window. This window I stored in a class "NewWindow" to quickly reproduce it.
In this "NewWindow" I have another button. If I press it the label of basic window should be updated and the window "NewWindow" should be closed automatically.
Here is my code:
from tkinter import *
class NewWindow(Toplevel):
def __init__(self, master = None):
super().__init__(master = master)
self.title('NewWindow')
self.lb = Label(self, text='Hello')
self.lb.grid(column=0, row=0, columnspan=1)
self.bt1 = Button(self, text="apply Hello", command= self.bt_press)
self.bt1.grid(column=0, row=1)
def bt_press(self):
window.basic_lb.text = "Hello"
window = Tk()
def new_Editor():
a = NewWindow(window)
window.title("BasicWindow")
window.basic_lb = Label(window, text='None')
window.basic_lb.grid(column=0, row=0, columnspan=1)
window.basic_bt = Button(window, text="NewWindow", command=new_Editor)
window.basic_bt.grid(column=0, row=1)
window.mainloop()
Problems:
At start both windows NewWindow and BasicWindow are displayd. I only want to open BasicWindow and NewWindow should be opened after button basic_bt is clicked. How can I solve it? (already solved by commed below)
Why the label text in basic_lb did not get some update after pressing self.bt1?
How is it possible to close NewWindow with use of bt_press method?
You have a few typos/errors in your code that are casuing some of your problems. As #Tim said, when you pass a function to a command like command=function(), it will be called on runtime, not when the button is pressed. You need to pass the function handle to the command, command=function. You got around this by using a lambda function in your button command, but it is easier to just have command=self.bt_press
Answering your second question, window.basic_lb.text = "Hello" is not how you change the text in a tkinter Label, use <Label>.config(text="Hello"). You also should use self.master and define self.master = master in __init__ instead of just using window, because while you can access window due to it not being defined in local scope, it's better to explicitly define it.
You can close a window using window.destroy().
Your working code is now:
from tkinter import *
class NewWindow(Toplevel):
def __init__(self, master = None):
super().__init__(master = master)
self.title('NewWindow')
self.master = master
self.lb = Label(self, text='Hello')
self.lb.grid(column=0, row=0, columnspan=1)
self.bt1 = Button(self, text="apply Hello", command=self.bt_press)
self.bt1.grid(column=0, row=1)
def bt_press(self):
self.master.basic_lb.config(text="Hello")
self.destroy()
window = Tk()
def new_Editor():
a = NewWindow(window)
window.title("BasicWindow")
window.basic_lb = Label(window, text='None')
window.basic_lb.grid(column=0, row=0, columnspan=1)
window.basic_bt = Button(window, text="NewWindow", command=new_Editor)
window.basic_bt.grid(column=0, row=1)
window.mainloop()
I don't get how to add the The Tkinter Menu Widget with my code I tried the code on a website but it won't work. It think im just being silly but please help. Just for the record I have looked at other stack overflow solutions but they did't work.
import pyautogui
import time
import tkinter as tk
class Coordinates():
replayBtn = (100,350)
class YourGUI(tk.Tk):
def __init__(self):
# inherit tkinter's window methods
tk.Tk.__init__(self)
#Enter X field and label ⬇
tk.Label(self, text="ENTER X:").grid(row=0, column=3)
self.inputX = tk.Entry(self)
self.inputX.grid(row=0, column=1)
#Enter Y field and label ⬇
tk.Label(self, text="ENTER Y:").grid(row=0, column=0)
self.inputY = tk.Entry(self)
self.inputY.grid(row=0, column=4)
# Start Button ⬇
tk.Button(self, text="start", command=self.do_conversion).grid(row=3, column=0, columnspan=2)
# close button ⬇
tk.Button(self, text="exit!", command=self.EXITME).grid(row=4, column=0, columnspan=2)
def EXITME(self):
exit(0) # crashed prog so it closes
# strtoint("crashmE!")
def do_conversion(self):
y = self.inputY.get()
x = self.inputX.get()
running = True
try:
x = int(x)
y = int(y)
except:
print("Invalid point")
exit(0)
# strtoint("crashmE!")
while running:
pyautogui.click(x, y)
if __name__ == '__main__':
your_gui = YourGUI()
your_gui.title('Macro Clicker') # Set title
your_gui.iconbitmap('favicon.ico') # Set icon
your_gui.resizable(False, False)
your_gui.mainloop()
time.sleep(0)
Sorry to say that but if you aren't lazy u will find over thousend examples about tkinter menu.
Even your code has nothing to do with tkinter.menu!?
Here's an example of mine. The "Info" menu is empty just to show you how create additional menus.
from tkinter import Tk, Frame, Menu
class Gui(Frame):
def __init__(self, master):
self.master = master
Frame.__init__(self, self.master) #main container
self.grid()
self.create_menu()
def create_menu(self):
self.menubar = Menu(self.master)
self.theme = Menu(self.menubar, tearoff=0)
self.menubar.add_cascade(label="View", menu=self.theme)
self.menubar.add_command(label="Info", command=self.passing)
self.views = Menu(self.theme, tearoff=0)
self.theme.add_cascade(label="Themes", menu=self.views)
self.views.add_command(label="Default", command=self.passing)
self.views.add_command(label="Red", command=self.passing)
self.views.add_command(label="Blue", command=self.passing)
self.views.add_command(label="Random", command=self.passing)
self.master.configure(menu=self.menubar)
def passing(self):
print("Use your brain before asking questions on stackoverflow or use google if your brain is slow")
if __name__ == "__main__":
root = Tk()
root.geometry("300x200")
my_gui = Gui(root)
root.mainloop()
I feel like I've scoured the web for an eternity, rephrased my question a thousand times for something I feel like should be very simple.
I wonder if there is a way to check if a Tkinter Widget is active (not greyed out / disabled). I have a set of OptionMenus that start out disabled, and are configured to state=ACTIVE when they click a checkbox, so that the user can select which OptionMenus they want to use.
When I try to "submit" the fields in the OptionMenus, I only want the ones that are ACTIVE. I already tried if OptionMenu.state == ACTIVE but then I get an error that OptionMenu has no attribute state, even though I configure that earlier.
Here is a sample of my code:
from tkinter import *
class Application(Frame):
# Initializing the window and the required variables
def __init__(self, master=None):
Frame.__init__(self, master)
self.checkbox_in_use = BooleanVar(self, False)
self.checkbox = Checkbutton(self, text="check",
var=self.checkbox_in_use,
command=self.check_change
self.checkbox.grid(row=0, column=1, sticky='W')
self.menu = OptionMenu(title_setting,
"Menu",
"Menu",
["Menu1", "Menu2"])
self.menu.grid(row=1, column=1)
self.menu.config(state=DISABLED)
submit = Button(self, text="submit",
command=self.submit_function)
submit.grid(row=2, column=0)
self.master = master
self.init_window()
# Initialize the window
def init_window(self):
self.master.title("Example")
self.pack(fill=BOTH, expand=1)
def check_change(self):
if self.checkbox_in_use.get():
self.menu.config(state=ACTIVE)
else:
self.menu.config(state=DISABLED)
def submit_function(self):
# This is the part I want to do something with.
if self.menu.state == ACTIVE:
print("You are good to go! Do the stuff.")
root = Tk()
root.geometry("400x300")
app = Application(root)
root.mainloop()
Thank you for all responses.
All you need is cget() for this. self.menu.cget('state') will do the trick.
That said I want to point out some other things in your code.
You Application class already has an __init__ at the start so why use:
# Initialize the window
def init_window(self):
self.master.title("Example")
self.pack(fill=BOTH, expand=1)
You really should not pack the frame from inside the frame class but rather when calling the class. Also pack wont work here it will throw an error. Do this instead: app = Application(root).grid().
Take a look at the reformatted example below (with cget()).
from tkinter import *
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master.title("Example")
self.checkbox_in_use = BooleanVar(self, False)
self.checkbox = Checkbutton(self, text="check", var=self.checkbox_in_use, command=self.check_change)
self.checkbox.grid(row=0, column=1, sticky='W')
self.menu = OptionMenu(master,"Menu","Menu",["Menu1", "Menu2"])
self.menu.grid(row=1, column=1)
self.menu.config(state=DISABLED)
Button(self, text="submit", command=self.submit_function).grid(row=2, column=0)
def check_change(self):
if self.checkbox_in_use.get():
self.menu.config(state=ACTIVE)
else:
self.menu.config(state=DISABLED)
def submit_function(self):
print(self.menu.cget('state'))
root = Tk()
root.geometry("400x300")
app = Application(root).grid()
root.mainloop()
I'm just getting started coding in Python/Tkinter for a small Pymol plugin. Here I'm trying to have a toggle button and report its status when it is clicked. The button goes up and down, but toggleAVA never gets called. Any ideas why?
from Tkinter import *
import tkMessageBox
class AVAGnome:
def __init__(self, master):
# create frames
self.F1 = Frame(rootGnome, padx=5, pady=5, bg='red')
# checkbuttons
self.AVAselected = IntVar()
self.AVAselected.trace("w", self.toggleAVA)
self.AVAbutton = Checkbutton(self.F1, text='AVA', indicatoron=0, variable=self.AVAselected)
# start layout procedure
self.layout()
def layout(self):
self.F1.pack(side=TOP, fill=BOTH, anchor=NW)
#entry and buttons
self.AVAbutton.pack(side=LEFT)
def toggleAVA(self, *args):
if (self.AVAselected.get()):
avastatus = "selected"
else:
avastatus = "unselected"
tkMessageBox.showinfo("AVA status", avastatus)
def __init__(self):
open_GnomeUI()
def open_GnomeUI():
# initialize window
global rootGnome
rootGnome = Tk()
rootGnome.title('AVAGnome')
global gnomeUI
gnomeUI = AVAGnome(rootGnome)
I tested your code with Pymol.
Problem is because you use Tk() to create your window. You have to use Toplevel() and then it will work correctly with trace() or with command=.
Pymol is created with tkinter which can have only one window created with Tk() - it is main window in program. Every other window has to be created with Toplevel().
I have attached a working version of your code below. You can refer to it to learn where you went wrong. Generally, you have to mind how you structure your code if you are using a class format.This will help you visualize your code and debug better. You can read this discussion to help you.
from Tkinter import *
import tkMessageBox
class AVAGnome(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
# create frames
self.F1 = Frame(self, padx=5, pady=5, bg='red')
# checkbutton
self.AVAselected = IntVar()
self.AVAselected.trace("w", self.toggleAVA)
self.AVAbutton = Checkbutton(
self.F1, text='AVA', indicatoron=0, width=10,
variable=self.AVAselected)
# start layout procedure
self.F1.pack(side=TOP, fill=BOTH, anchor=NW)
self.AVAbutton.pack(side=LEFT) #entry and buttons
def toggleAVA(self, *args):
if (self.AVAselected.get()):
avastatus = "selected"
else:
avastatus = "unselected"
tkMessageBox.showinfo("AVA status", avastatus)
if __name__ == '__main__':
rootGnome = Tk()
rootGnome.title('AVAGnome')
gnomeUI = AVAGnome(rootGnome)
gnomeUI.pack(fill="both", expand=True)
gnomeUI.mainloop()
Update: The above code structure is for standalone tkinter programme. I am attempting to convert this working code to follow Pymol plugin example. Revised code is posted below and is susceptible to further revision.
# https://pymolwiki.org/index.php/Plugins_Tutorial
# I adapted from the example in the above link and converted my previous code to
#
from Tkinter import *
import tkMessageBox
def __init__(self): # The example had a self term here.
self.open_GnomeUI()
class AVAGnome(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
# create frames
self.F1 = Frame(self, padx=5, pady=5, bg='red')
# checkbutton
self.AVAselected = IntVar()
self.AVAselected.trace("w", self.toggleAVA)
self.AVAbutton = Checkbutton(
self.F1, text='AVA', indicatoron=0, width=10,
variable=self.AVAselected)
# start layout procedure
self.F1.pack(side=TOP, fill=BOTH, anchor=NW)
self.AVAbutton.pack(side=LEFT) #entry and buttons
def toggleAVA(self, *args):
if (self.AVAselected.get()):
avastatus = "selected"
else:
avastatus = "unselected"
tkMessageBox.showinfo("AVA status", avastatus)
# Note, I added a "self" term throughout function.
# Try w/ & w/o "self" to see which works.
def open_GnomeUI(self):
self.rootGnome = Tk()
self.rootGnome.title('AVAGnome')
self.gnomeUI = AVAGnome(self.rootGnome)
self.gnomeUI.pack(fill="both", expand=True)
self.gnomeUI.mainloop()
I am trying to make second menu button next to the one i already made but no matter what code i use, i cannot make a second one. I want to place it next the first button by using .grid() not .place()
heres the code
from tkinter import *
import tkinter.messagebox
import ctypes
class Application(Frame):
def __init__(self, master):
Frame.__init__(self,master)
self.grid()
self.create_widgets()
def create_widgets(self):
mb = Menubutton (self, text="Sniper Rifle", relief=RAISED)
mb.menu = Menu (mb, tearoff = 0)
mb["menu"] = mb.menu
self.m40a5Var = IntVar()
self.srrVar = IntVar()
mb.menu.add_checkbutton (label="M40A5", variable=self.m40a5Var)
mb.menu.add_checkbutton (label="SRR", variable=self.srrVar)
mb.grid()
root = Tk()
root.title("heeey")
root.geometry("180x100")
app = Application(root)
root.mainloop()
I don't understand the question. You say that no matter what you try you can't create a second button. All you have to do to create another button is... create another button:
mb = Menubutton(...)
another_mb = Menubutton(...)
...
mb.grid(row=0, column=0)
another_mb.grid(row=0, column=1)