1

I'm working on developing a python application for Ubuntu that enables a user to have their desired resolution without requiring graphics drivers.
In order to do this I've been using xrandr.
However I've now encountered a problem, despite searching for others with similar problems and attempting the fixes.

Here is my code (I'm new to Python, this being my first application, so the code will probably be very messy and inefficient to others - I also need to relocate some of the code, but hopefully it's readable):

#!/usr/bin/python

import gtk, sys
import os, commands     # enables us to use xrandr

class ResolutionX(gtk.Window):

    # create an instance of ResolutionX
    def __init__(self):
        # constructor
        super(ResolutionX, self).__init__()

        # set the default window values
        self.set_title("ResolutionX")
        self.set_size_request(600, 200)
        self.set_position(gtk.WIN_POS_CENTER)

        fix = gtk.Fixed()

        # resolution combo box
        ResComboB = gtk.combo_box_new_text()
        ResComboB.append_text('1024 x 768')
        ResComboB.append_text('1280 x 960')
        ResComboB.append_text('1366 x 768')
        ResComboB.append_text('1280 x 1024')
        ResComboB.append_text('1440 x 900')
        ResComboB.append_text('1440 x 960')
        ResComboB.append_text('1440 x 1080')
        ResComboB.append_text('1600 x 1200')
        ResComboB.append_text('1920 x 1080')
        ResComboB.append_text('1920 x 1200')
        ResComboB.set_active(0)
        ResComboB.connect("changed", ResComboB_changed)

        label = gtk.Label("Resolution:")

        # apply button
        applyBtn = gtk.Button("Apply")
        applyBtn.set_size_request(110, 29)
        applyBtn.connect("clicked", applyBtn_on_clicked)

        # add widgets to the main window
        fix.put(ResComboB, 98, 78)
        fix.put(label, 30, 85)
        fix.put(applyBtn, 470, 78)
        self.add(fix)

        self.connect("destroy", gtk.main_quit)
        self.show_all()


def ResComboB_changed(ResComboB):
    string = ResComboB.get_active_text()
    stringRes = string.replace("x", "")
    echo = "echo "
    stringConfig = os.popen("cvt " + stringRes).readlines()

    # TODO: create a hidden directory in /home folder

    # create the file [config.sh]
    fWrite = open('config.sh', 'w')
    # write the xrandr configuration into the file so we can edit it
    print >> fWrite, stringConfig
    fWrite.close()

    fRead = open('config.sh', 'r')

    # TODO: remove last 4 characters from readString // DONE

    # get input from [config.sh] and edit  according to the choice of resolution
    if (string=="1024 x 768"):
        for line in fRead:
            storedString = line
            readString = storedString[83:]
            readString = readString[:76]
            print readString # verification
            resolution = readString[:16]
    if (string=="1366 x 768"):
        for line in fRead:
            storedString = line
            readString = storedString[76:]
            readString = readString[:76]
            print readString # verification
            resolution = readString[:16]
    if (string=="1280 x 960") or (string=="1440 x 900"):
        for line in fRead:
            storedString = line
            readString = storedString[84:]
            readString = readString[:77]
            print readString # verification
            resolution = readString[:16]
    if (string=="1440 x 960"):
        for line in fRead:
            storedString = line
            readString = storedString[77:]
            readString = readString[:76]
            print readString # verification
            resolution = readString[:16]
    if (string=="1280 x 1024") or (string=="1440 x 1080") or (string=="1600 x 1200") or (string=="1920 x 1080") or (string=="1920 x 1200"):
        for line in fRead:
            storedString = line
            readString = storedString[85:]
            readString = readString[:81]
            print readString # verification
            resolution = readString[:17]
    os.system("xrandr --newmode " + readString)

    fRead.close()

def applyBtn_on_clicked(applyBtn):

    # detect and store monitor output type - eg. VGA1, DVI-0
    monitorType = os.popen("xrandr | grep ' connected ' | awk '{ print$1 }'").readlines()
    fWrite = open('config.sh', 'w')
    print >> fWrite, monitorType
    fWrite.close()
    # get the input from [config.sh]
    fRead = open('config.sh', 'r')
    for line in fRead:
        CmonitorType = line
    fRead.close()
    # edit CmonitorType so we can use it
    CmonitorType = CmonitorType[2:]
    CmonitorType = CmonitorType[:7]
    print CmonitorType # verification

    os.system("xrandr --addmode " + CmonitorType + resolution)

ResolutionX()
gtk.main()

I get this error:
Traceback (most recent call last): File "file.py", line 129, in applyBtn_on_clicked os.system("xrandr --addmode " + CmonitorType + resolution) NameError: global name 'resolution' is not defined

How can I solve this problem?

0

1 Answer 1

2

Make the following changes to your script:

Below line def ResComboB_changed(ResComboB): add the following to it, so it looks like this:

def ResComboB_changed(ResComboB):
    global resolution

or add resolution = "" above the def line like this:

resolution = ""
def ResComboB_changed(ResComboB):

Hope that helps ;)

1
  • Isn't using global parameters bad practice though?
    – TellMeWhy
    Apr 26, 2016 at 7:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.