if i click noticeall in menubar, i wanna it to be displayed on textarea at mainframe.py
how can i write code in def noticeall.. plz
main.py file code is
import mainframe
mainframe.show()
mainframe.py file code is
def show ():
frame = tkinter.Tk()
frame.title('test')
frame.geometry('800x600')
frame.resizable(False, False)
sf = ScrolledFrame(frame, width=800, height=600)
sf.pack(side="top", expand=1, fill="both")
sf.bind_arrow_keys(frame)
sf.bind_scroll_wheel(frame)
menu_bar = menubar.make_menubar(frame,)
frame.config(menu=menu_bar)
inner_frame = sf.display_widget(Frame)
entry = Entry(inner_frame, width=750)
entry.pack()
entry.focus_set()
textarea = Text(inner_frame, width=750, height=200)
textarea.pack()
frame.mainloop()
menubar.py file code is
def make_menubar(frame):
menubar = Menu(frame)
file_menu = Menu(menubar, tearoff=0)
file_menu.add_command(label="noticeall",command=noticeall)
def noticeall():
notice_list = ncontroller.select_all()
print_list(notice_list)'
plz recommend next line code
print_list.insert(textarea)??? - i cant, textarea is local variable in mainframe.py
def make_menubar(frame, textarea):
menubar = Menu(frame)
file_menu = Menu(menubar, tearoff=0)
file_menu.add_command(label="noticeall", command=lambda: noticeall(textarea))
return menubar
def noticeall(textarea):
notice_list = ncontroller.select_all()
textarea.insert(notice_list)
In fact, the best way is to create a class. Pass parameters through self.
Related
I was trying to make a application by tkinter, and I wanted to make a checkbutton in menubar. However, I don't know how to make the checkbutton already checked when I run the code.
Here's my code
import tkinter as tk
def func():
#some code here
var = tk.BooleanVar
win = tk.Tk()
menubar = tk.Menu(win)
optmenu = tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label='Options', menu=optmenu)
optmenu.add_checkbutton(label='xyz', variable=var, onvalue=1, offvalue=0, command=func)
win.config(menu=menubar)
win.mainloop()
use this code below:
import tkinter as tk
def func():
pass
#some code here
win = tk.Tk()
var = tk.StringVar(win,'on')
menubar = tk.Menu(win)
optmenu = tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label='Options', menu=optmenu)
optmenu.add_checkbutton(label='xyz', variable=var, onvalue='on', offvalue='off', command=func)
win.config(menu=menubar)
win.mainloop()
have fun :)
I am trying to use a tkinter Toplevel window with a Text box to post to a Text box in the main window, but I keep getting the following error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\someone\ouch\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "<ipython-input-14-df46ff30dac4>", line 21, in GetFromDIALOG
X = littletext.get("1.0","end")
NameError: name 'littletext' is not defined
In my script below, I call the Text widget in the Toplevel "littletext" and the Text widget in the main window "BIGTEXT". I also tried setting the Text boxes as StringVars, as most tutorials and scripts seem to suggest is right, but StringVars seem to work more on Entry (not Text) boxes.
I am wondering if I should use a custom class instead of a Toplevel, although I am not sure how I would handle that would work either.
My code is below.
import tkinter as tk
def openDIALOG(window):
DIALOG = tk.Toplevel(window)
DIALOG.title("DIALOG")
DIALOG.geometry("400x300+100+100")
littletext = tk.Text(DIALOG, height=15)
littletext.pack()
btn1 = tk.Button(DIALOG, text ="POST TO MAIN WINDOW", command=GetFromDIALOG)
btn1.pack(side="left")
btn2 = tk.Button(DIALOG, text ="EXIT", command = DIALOG.destroy)
btn2.pack(side="left")
def GetFromDIALOG ():
X = littletext.get("1.0","end")
print(X)
window = tk.Tk()
window.title("main")
menubar = tk.Menu(window)
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label="RUN A DIALOG", command=lambda:openDIALOG(window))
filemenu.add_command(label="Exit", command=window.destroy)
menubar.add_cascade(label="FILE", menu=filemenu)
window.config(menu=menubar)
BIGTEXT = tk.Text(window)
BIGTEXT.pack()
BUTTON = tk.Button(window, text="Post", command=openDIALOG)
BUTTON.pack()
window.mainloop()
One way to fix the NameError, which is due it it being a local variable in the openDIALOG() function in your code, is to make it an attribute of instances of the class. This is better than using a global variable, which is another option, but global variables are bad.
Stackoverflow is not intended to replace existing tutorials or documentation, so I'll describe only very briefly what the demo code below it doing. This topic would be covered much more thoroughly in most of the Python tutorials that are available online, if you took time to for them.
So, here's how that might be done based on the code in your question. Notice how littletext is now self.littletext. This is because it has been turned into a class attribute of the class MY_DIALOG that's been defined, so it can be access through the self argument which will automatically be passed as the first argument of all the class' functions (which are know as class "methods").
import tkinter as tk
from tkinter.constants import *
class MyDialog:
def __init__(self, window):
DIALOG = tk.Toplevel(window)
DIALOG.title("DIALOG")
DIALOG.geometry("400x300+100+100")
self.littletext = tk.Text(DIALOG, height=15)
self.littletext.pack()
btn1 = tk.Button(DIALOG, text="POST TO MAIN WINDOW", command=self.GetFromDIALOG)
btn1.pack(side="left")
btn2 = tk.Button(DIALOG, text="EXIT", command=DIALOG.destroy)
btn2.pack(side="left")
def GetFromDIALOG(self):
X = self.littletext.get("1.0", END)
print(X)
if __name__ == '__main__':
window = tk.Tk()
window.title("main")
menubar = tk.Menu(window)
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label="RUN A DIALOG", command=lambda: MyDialog(window))
filemenu.add_command(label="Exit", command=window.destroy)
menubar.add_cascade(label="FILE", menu=filemenu)
window.config(menu=menubar)
BIGTEXT = tk.Text(window)
BIGTEXT.pack()
BUTTON = tk.Button(window, text="Post", command=lambda: MyDialog(window))
BUTTON.pack()
window.mainloop()
Thanks martineau! :)
There was a few small problems I cleaned up.
The dialog you coded didnt post back to BIGTEXT in the main window, so I completed the callback aspect of it.
The BUTTON in the footer of the main window was actually a mistake on my part. I accidentally forgot to delete it in the original post, so I deleted it here and let the menu work the call.
Final code is below.
import tkinter as tk
from tkinter.constants import *
class MY_DIALOG:
def __init__(self, window):
DIALOG = tk.Toplevel(window)
DIALOG.title("DIALOG")
DIALOG.geometry("400x300+100+100")
self.littletext = tk.Text(DIALOG, height=15)
self.littletext.pack()
btn1 = tk.Button(DIALOG, text="POST TO MAIN WINDOW", command=self.GetFromDIALOG)
btn1.pack(side="left")
btn2 = tk.Button(DIALOG, text="EXIT", command=DIALOG.destroy)
btn2.pack(side="left")
def GetFromDIALOG(self):
X = self.littletext.get("1.0","end")
BIGTEXT.insert("end", X)
self.littletext.delete("1.0","end")
if __name__ == '__main__':
window = tk.Tk()
window.title("main")
menubar = tk.Menu(window)
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label="RUN A DIALOG", command=lambda: MY_DIALOG(window))
filemenu.add_command(label="Exit", command=window.destroy)
menubar.add_cascade(label="FILE", menu=filemenu)
window.config(menu=menubar)
BIGTEXT = tk.Text(window)
BIGTEXT.pack()
window.mainloop()
Does anyone know why it will not show the taskbar in my code. I am trying to get the top to say Exit and information in the file drop down menu. I am kind of new to tkinter and i just need a bit of help please. Also if you have any suggestions on how to improve it, i would really appreciate it!
My code is below:
from time import sleep
from tkinter import *
from tkinter import messagebox, ttk, Tk
root = Tk()
class GUI():
def taskbar(self):
menu = Menu(root)
file = Menu(menu)
file.add_command(label="Exit", command=self.exit_GUI)
file.add_command(label = "Information", command=self.info_popup)
def Main_Menu(self):
topFrame = Frame(root)
topFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack(side=BOTTOM)
Income_button = Button(topFrame, text="Enter your incomes", command=self.Income)
Expense_button = Button(topFrame, text="Enter your expenses", command=self.Expense)
Total_button = Button(bottomFrame, text="View Results", command=self.Total)
Income_button.pack()
Expense_button.pack()
Total_button.pack()
def Income(self):
pass
def Expense(self):
pass
def Total(self):
pass
def exit_GUI(self):
exit()
def info_popup(self):
pass
g = GUI()
g.taskbar()
g.Main_Menu()
g.Income()
g.Expense()
g.Total()
g.info_popup()
root.mainloop()
You need to change the taskbar function to:
def taskbar(self):
menu = Menu(root)
file = Menu(menu)
file.add_command(label="Exit", command=self.exit_GUI)
file.add_command(label = "Information", command=self.info_popup)
root.config(menu=file)
This will tell the window to use the menubar.
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=...)
I have created an coding gui,which doesnot show 'File' and 'save' in the Gui
Please help me to fix my problem.
I have created a Function for File and Save ,still not working!
please help me to rectify my code!
from tkinter import *
import tkinter.messagebox
import tkinter
import tkinter as tki
import tkinter.filedialog as th1
import re
class App(object):
def __init__(self,root):
self.root = root
# create a Frame for the Text and Scrollbar
txt_frm = tki.Frame(self.root, width=600, height=400)
txt_frm.pack(fill="both", expand=True)
# ensure a consistent GUI size
txt_frm.grid_propagate(False)
# create first Text label, widget and scrollbar
self.lbl1 = tki.Label(txt_frm, text="Type")
self.lbl1.grid(row=0,column=0,padx=2,pady=2)
self.txt1 = tki.Text(txt_frm, borderwidth=3, relief="sunken", height=4,width=55)
self.txt1.config(font=("consolas", 12), undo=True, wrap='word')
self.txt1.grid(row=0, column=1, sticky="nsew", padx=2, pady=2)
scrollb1 = tki.Scrollbar(txt_frm, command=self.txt1.yview)
scrollb1.grid(row=0, column=2, sticky='nsew')
self.txt1['yscrollcommand'] = scrollb1.set
button = tki.Button(txt_frm,text="Clickone", command = self.retrieve_input)
button.grid(column=2,row=0)
button1 = tki.Button(txt_frm,text="Cli", command = self.clearBox)
button1.grid(column=2,row=0)
def retrieve_input(self):
input1 = self.txt1.get("0.0",'end-1c')
with open('text.txt','a+') as f:
f.write(input1+'\n')
f.close()
def clearBox(self):
self.txt1.delete('1.0', 'end')#<-0.0/1.0
def file_save():
f = th1.asksaveasfile(mode='w', defaultextension=".txt")
if f is None: # asksaveasfile return `None` if dialog closed with "cancel".
return
text2save = str(text.get(1.0, END))
a= (f)
f.write(text2save)
f.close()
root = tki.Tk()
menubar=Menu(root)
filemenu=Menu(menubar,tearoff=0)
filemenu.add_command(label="Save", command=file_save)
app = App(root)
root.mainloop()
Please help!Answers will be appreciated!
You aren't adding the menubar to the window. Add this after you create the menubar.
root.configure(menu=menubar)
You then also have to add the file menu to the menubar:
menubar.add_cascade(label="File", menu=filemenu)