I'm trying to learn how to make a GUI with tkinter the conde is rather basic but I get the following error:
Exception has occurred: TclError
no display name and no $DISPLAY environment variable
File "/home/josh/Documents/VSC/python/SECOM/mainWindow.py", line 7, in __init__
self.wind = Tk()
File "/home/josh/Documents/VSC/python/SECOM/mainWindow.py", line 12, in <module>
MW = mainWindow()
When I google such error there is only answer for raspberry pi or remote servers and stuff. Im just using ubuntu(20.04) and have a conda (4.8.3) venv with python (3.8). I'm also using VSC and have the venv as interpreter in VSC. HELP :c
MainWindow.py
from tkinter import ttk
from tkinter import *
class mainWindow:
def __init__(self):
self.title = "SECOM"
self.wind = Tk()
self.wind.title(self.title)
if __name__ == '__main__':
MW = mainWindow()
window.mainloop()
You are talking a lot about windows in your code, but not much of anything is actually a window. Some of it is nothing, at all. Try This.
import tkinter as tk
class Root(tk.Tk):
def __init__(self, **kwargs):
tk.Tk.__init__(self, **kwargs)
#PUT YOUR APP HERE
if __name__ == '__main__':
root = Root()
root.title("SECOM")
root.mainloop()
Here are the problems with your script
from tkinter import ttk
#importing like this pollutes your namespace
from tkinter import *
class mainWindow:
def __init__(self):
#there is no reason to store a reference to the title
self.title = "SECOM"
#why are you burying your root in a class property
self.wind = Tk()
#this is why you don't need a reference to title
self.wind.title(self.title)
if __name__ == '__main__':
#sure
MW = mainWindow()
#window? window where? You buried this in MW.wind
window.mainloop()
Related
I'm trying to keep my code clean by separating the GUI from the logic.
Inside of the 'main.py' file, I'd like to call functions from other files that are imported to build the GUI.
The problem is that I cannot figure out how to build a GUI from the 'main.py' file when I try to call another file as an imported module.
Here's what I have in the 'main.py' file:
from tkinter import *
import create_btn
class Main(Tk):
def __init__(self):
super().__init__()
self.title('Main Window')
self.geometry('600x400')
self.eval('tk::PlaceWindow . center')
if __name__ == '__main__':
app = Main()
app.mainloop()
And here is what I have in the 'create_btn.py' file:
from tkinter import *
def createBTN(self):
self.b1 = Button(root, text='B1')
self.b1.pack()
So, how exactly can I build a simple button from another file that I want to import into the 'main.py', or in other words, how do I get the 'create_btn.py' file to build the button inside of the 'main.py' file? A simple example would be greatly appreciated.
I have completely rewritten your code:
# main.py
from tkinter import *
import create_btn
class Main_app:
def __init__(self):
self.root = Tk()
self.root.title("Main Window")
self.geometry("600x400")
self.eval('tk::PlaceWindow . center')
self.button = create_btn.create(self.root)
self.root.mainloop()
a = Main_app()
# create_btn.py
from tkinter import *
class create:
def __init__(self, root):
btn = Button(root, text="Button")
btn.pack()
return btn
This way you can edit the button later in main.py
I'm pretty new in Python. I'm trying to develop a GUI using a drag and drop designer called PAGE (http://page.sourceforge.net/html/index.html) based on Tkinter.
After having created a new project, I added a button to a window. The following code is autogenerated by PAGE and saved into a file called 'myGui.py':
import sys
import tkinter as tk
import tkinter.ttk as ttk
from tkinter.constants import *
import myGui_support
class Toplevel1:
def __init__(self, top=None):
...
self.StartButton = tk.Button(self.top)
self.StartButton.configure(command=myGui_support.StartButton_Callback)
...
def start_up():
myGui_support.main()
if __name__ == '__main__':
myGui_support.main()
PAGE creates also a second file called 'myGui_support.py', in which after some import statements, the following code is included:
import myGui
def main(*args):
'''Main entry point for the application.'''
global root
root = tk.Tk()
root.protocol( 'WM_DELETE_WINDOW' , root.destroy)
# Creates a toplevel widget.
global _top1, _w1
_top1 = root
_w1 = myGui.Toplevel1(_top1)
root.mainloop()
if __name__ == '__main__':
myGui.start_up()
def StartButton_CallBack():
_w1.DebugLabel["text"] = 'START BUTTON PRESSED'
def StopButton_CallBack():
_w1.DebugLabel["text"] = 'STOP BUTTON PRESSED'
Once I run the myGui in Spyder, an Attribute error occurres:
AttributeError: module 'myGui_support' has no attribute 'StartButton_Callback'
How can it be possible? I already defined the callback function into the 'myGui_support.py'. Any suggestions?
from tkinter import*
import tkinter.messagebox
from tkinter import ttk
import random
import time
import datetime
def main():
root = Tk()
app = Login(root)
class Login:
def __init__(self, master):
self.master = master
self.master.title("Billing Login System")
self.master.geometry("1350x750+0+0")
self.master.config(bg = 'cadet blue')
self.frame = Frame(self.master,bg='cadet blue')
self.frame.pack()
#Some code here
..(Login Conditions)
..
#
#After authentication this window should pop up
class customer:
def __init__(self, root):
self.root = root
self.root.title("eZ Billing System")
self.root.geometry("1350x750+0+0")
self.root.config(bg="cadet blue")
self.frame = Frame(self.root,bg='cadet blue')
self.frame.pack()
#some code here
if __name__ == '__main__':
main()
This code works but the problem is that when i run the file no error shows up or warnings and neither does any window shows but if i run any other python program then this windows pops up and no problems.
I am new to this and cant figure out whats wrong.
I guess you aren't creating main window instance.
That's why it is not showing error but not showing any output even.
Try adding this :
root.mainloop()
I am attempting to implement unittests (while learning about them) for a Tkinter application that I wrote. For that purpose I have managed to create a very minimalistic unittest around the basic structure of my application as shown below (passing it a Tkinter instance as master):
classTest.py
#! /usr/bin/env python
from Tkinter import *
class App():
def __init__(self,master):
self.text = "foo"
self.printy()
def printy(self):
print self.text
return "test"
# Call the main app
if __name__ == "__main__":
root = Tk()
app = App(root)
root.mainloop()
test.py
#! /usr/bin/env python
from Tkinter import *
import unittest
import classTest
class testTest(unittest.TestCase):
def test(self):
a = classTest.App(Tk())
self.assertEqual(a.printy(),"test")
if __name__ == "__main__":
unittest.main()
This test returns that it ran succesful, although it does print foo twice. However, when I then attempt to run the same concept on my whole application it crashes on the __init__ of my class.
unitTest.py
#! /usr/bin/env python
import unittest
from Tkinter import *
import MassyTools
class readTest(unittest.TestCase):
def test(self):
a = MassyTools.App(Tk())
self.assertEqual(a.readData("./tests/calibrated_IBD cohort sample H6-run 1_0_E24_1.xy")[0][0],1299.11)
if __name__ == "__main__":
unittest.main()
Running this test crashes it on the fact that root is not defined (see below error).
I have been fiddling around with mocking as per this blogpost but I don't really grasp the concept yet. Below is a subset of the MassyTools.py file including the offending self.toolbar line:
class App():
def __init__(self, master):
# The Canvas
self.canvas = FigureCanvasTkAgg(self.fig, master=master)
self.toolbar = NavigationToolbar2TkAgg(self.canvas, root)
self.canvas.get_tk_widget().pack(fill=BOTH, expand=YES)
self.canvas.draw()
Therefore, the question is if I should instantiate it differently in the unittest, modify the application to have a fake root if called in this way or something else entirely.
I found the issue in the application that I wanted to test, I declared root = Tk() in the main. This root was passed to the App class as master but in the program I made a call to root where it should have been to master. The below changes removes the initial crash, I also included a call to atexit to not have to physically close the application (as per this answer).
Revised application:
class App():
def __init__(self, master):
# The Canvas
self.canvas = FigureCanvasTkAgg(self.fig, master=master)
self.toolbar = NavigationToolbar2TkAgg(self.canvas, master)
self.canvas.get_tk_widget().pack(fill=BOTH, expand=YES)
self.canvas.draw()
# Call the main app
if __name__ == "__main__":
root = Tk()
app = App(root)
root.mainloop()
I adjusted my unittest.py code as follows, which now finishes with an OK after i physically close the application:
#! /usr/bin/env python
import unittest
from Tkinter import *
import MassyTools
import atexit
class readTest(unittest.TestCase):
def test(self):
self.assertEqual(a.readData("./tests/calibrated_IBD cohort sample H6-run 1_0_E24_1.xy")[0][0],1299.11)
if __name__ == "__main__":
# Set up Tk(), instantiate the application and close it right away.
root = Tk()
a = MassyTools.App(root)
atexit.register(root.mainloop)
root.destroy()
# Start the actual tests
unittest.main(verbosity=2)
I learned the book "programming python' these days. When I run the examples, I met the problem. The shell showed me the error:
AttributeError: 'NoneType' object has no attribute 'pack'
However, I copy the exactly code from the book. I'm a freshman of Python. I try to fix it by myself, but I failed. So I hope anyone could kindly help me.
Thanks !!!!!!
CODEļ¼
#File test.py
from tkinter import *
from tkinter.messagebox import showinfo
def MyGui(Frame):
def __init__(self, parent = None):
Frame.__init__(self, parent)
button = Button(self, text='press', command=reply)
button.pack()
def reply(self):
showinfo(title = 'popup',message ='Button pressed!')
if __name__ == '__main__':
window = MyGui()
window.pack()
window.mainloop()
#File test2.py
from tkinter import *
from test import MyGui
mainwin = Tk()
Label(mainwin,text = __name__).pack()
popup = Toplevel()
Label(popup,text = 'Attach').pack(side = LEFT)
MyGui(popup).pack(side=RIGHT)
mainwin.mainloop()
You can fix this with the following code:
#File test.py
from tkinter import *
from tkinter.messagebox import showinfo
class MyGui(Frame):
def __init__(self, parent = None):
Frame.__init__(self, parent)
button = Button(self, text='press', command=self.reply)
button.pack()
def reply(self):
showinfo(title = 'popup',message ='Button pressed!')
if __name__ == '__main__':
window = MyGui()
window.pack()
window.mainloop()
Basically two small syntax errors. First you were trying to make a class of MyGui, but you used keyword def which made a function instead (that returned None, hence the error you received.) It is syntaxically correct in python to define functions inside of functions so it was a little harder to catch. You have to use the keyword class to define a class.
Secondly when referencing the function reply you must use self.reply within the class itself.